Android 6(M)权限问题(创建目录不起作用)

Jem*_*rov 14 android android-camera android-6.0-marshmallow

我有这个代码用于创建保存图片的目录:

        File storageDir = null;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {                
            storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "myphoto");
            if (!storageDir.mkdirs()) {                    
                if (!storageDir.exists()){                       
                    Log.d("photo", "failed to create directory");
                    return null;
                }
            }
        }
        return storageDir;
Run Code Online (Sandbox Code Playgroud)

storeDir"/storage/emulated/0/Pictures/myphoto/"在android 6下面返回,在android 6上它返回null.

我有许可 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

buildToolVersion 23 targetSdkVersion 23

怎么修?

Jem*_*rov 22

正如@CommonsWare所回答的那样,在Android M上有运行时权限询问概念,因此在新方法中,在安装应用程序时,但在尝试使用在运行时请求权限的手机的特定功能时,不会询问权限.用户以后也可以禁用权限phone settings->app->yourapp->permissions.因此,在使用该权限执行某些操作之前,您必须先检查并询问用户:

int REQUEST_WRITE_EXTERNAL_STORAGE=1;
////...
        File storageDir = null;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            //RUNTIME PERMISSION Android M
            if(PackageManager.PERMISSION_GRANTED==ActivityCompat.checkSelfPermission(context,Manifest.permission.WRITE_EXTERNAL_STORAGE)){
                storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "myPhoto");
            }else{
                requestPermission(context);
            }    

        } 
        return storageDir;
////...
        private static void requestPermission(final Context context){
        if(ActivityCompat.shouldShowRequestPermissionRationale((Activity)context,Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            // Provide an additional rationale to the user if the permission was not granted
            // and the user would benefit from additional context for the use of the permission.
            // For example if the user has previously denied the permission.

            new AlertDialog.Builder(context)
                    .setMessage(context.getResources().getString(R.string.permission_storage))
                    .setPositiveButton(R.string.tamam, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    ActivityCompat.requestPermissions((Activity) context,
                            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                            REQUEST_WRITE_EXTERNAL_STORAGE);
                }
            }).show();

        }else {
            // permission has not been granted yet. Request it directly.
            ActivityCompat.requestPermissions((Activity)context,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    REQUEST_WRITE_EXTERNAL_STORAGE);
        }
    }

///...

    @Override
    public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {
        switch (requestCode) {
            case UtilityPhotoController.REQUEST_WRITE_EXTERNAL_STORAGE: {
                if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(context,
                            getResources().getString(R.string.permission_storage_success),
                            Toast.LENGTH_SHORT).show();

                } else {
                    Toast.makeText(context,
                            getResources().getString(R.string.permission_storage_failure),
                            Toast.LENGTH_SHORT).show();
                    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
                }
                return;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)


Com*_*are 16

您在Android 6.0+环境中运行此操作,并且您有targetSdkVersion23个.

在这种情况下,WRITE_EXTERNAL_STORAGEAndroid 6.0运行时权限系统的一部分.修改您的应用程序以参与此系统,或将您的应用程序targetSdkVersion降至23以下.

  • @JemshitIskenderov:我的意思是我写的.我对[Android 6.0运行时权限系统上的文档](http://developer.android.com/preview/features/runtime-permissions.html)的回答中有一个链接.这是相当漫长和涉及的.我的建议是暂时将你的`targetSdkVersion`放到23以下. (3认同)