自学内容网 自学内容网

android T 建立文件夹及文件的记录

第一:AndroidManifest.xml 中整体给予apk权限,如此加入后,在android的settings中,可以找到app.手动给予静态的权限,但是app不一定能使用,请大神指导为什么?

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
    <uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
    <uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
    <uses-permission android:name="android.permission.READ_CALENDAR" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

第二:添加rxPermisson的依赖
          添加依赖:
    打开 build.gradle(app) ,添加以下代码:
    implementation 'com.github.tbruyelle:rxpermissions:0.12'
    //rxJava 注意:这里必须是rxJava3,其它不行,反正我搞了半天
    api 'io.reactivex.rxjava3:rxjava:3.0.4'
    api 'io.reactivex.rxjava3:rxandroid:3.0.0'

第三步:
打开settings.gradle添加以下代码
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven {url 'https://jitpack.io'} //添加的依赖的仓。添加这句
    }
}

第四步:app将要动态申请的权限,获得用户允许后,就可以使用这些权限。
    private static final String[] permissionsGroup = new String[]{
            Manifest.permission.READ_MEDIA_IMAGES,
            Manifest.permission.READ_MEDIA_AUDIO,
            Manifest.permission.READ_MEDIA_VIDEO,
            Manifest.permission.READ_CALENDAR, //ok
            Manifest.permission.RECORD_AUDIO,  //ok
            Manifest.permission.ACCESS_FINE_LOCATION, //ok
            Manifest.permission.CAMERA //ok
    };


第五步:申请权限
    这里时点击onClick执行获取权限。
    public void testEnsureEachCombined(View view) {
        RxPermissions rxPermissions = new RxPermissions(this);
        Observable.timer(10, TimeUnit.MILLISECONDS)
                .compose(rxPermissions.ensureEachCombined(permissionsGroup))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Throwable {
                        Log.i(TAG, "权限名称:" + permission.name + ",申请结果:" + permission.granted);
                    }
                });
    }

第六步:在app中建立文件夹与文件
                File appDirectory = mContext.getFilesDir();
                File customDirectory = new File(appDirectory,"xuxdTest");
                if (!customDirectory.exists()){
                    boolean isDirectoryCreated = customDirectory.mkdirs();
                    if(isDirectoryCreated){
                        Log.d(TAG, "init-step-7 isDirectoryCreated "+isDirectoryCreated);
                    }else {
                        Log.d(TAG, "init-step-8 isDirectoryCreated "+isDirectoryCreated);
                    }
                }

                if(customDirectory.exists()){
                    Log.d(TAG, "init-step-9 customDirectory  exists"+customDirectory.toString());
                }else {
                    Log.d(TAG, "init-step-9 customDirectory  not exists");
                }

//                mWindEar.init(mContext);
//                mWindEar.startRecord(true);

                try {
                    File newFile = new File(customDirectory, "example.txt");

                    // 如果文件不存在,创建新文件
                    if (!newFile.exists()) {
                        boolean isFileCreated = newFile.createNewFile();
                        if (isFileCreated) {
                            Log.d(TAG, "文件建立成功 .......... ");
                        } else {
                            Log.d(TAG, "文件建立 失败 .........");
                        }
                    }
                } catch (IOException e) {
                    Log.e(TAG, "Error occurred: " + e.toString());
                }
            }
        });


原文地址:https://blog.csdn.net/u010689853/article/details/145154988

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!