【问题标题】:Android 6(M) permission issue (create directory not working)Android 6(M) 权限问题(创建目录不起作用)
【发布时间】:2015-11-20 10:14:48
【问题描述】:

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

        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;

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

我有权限<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

buildToolVersion 23 targetSdkVersion 23

如何解决?

【问题讨论】:

  • "storeDir 在 android 6 下返回 "file:///storage/emulated/0/Pictures/myphoto/",在 android 6 上返回 "file:///""——代码是返回 File 对象,而不是字符串。除此之外,你的targetSdkVersion 是什么?
  • 它实际上返回null

标签: android android-camera android-6.0-marshmallow


【解决方案1】:

正如@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;
            }
        }
    }

【讨论】:

  • 我在模拟器中测试过,如果你给了运行时权限,是否需要杀死进程并重新启动?
  • @ParaSara 不,你不需要
  • 我遇到了同样的问题:stackoverflow.com/questions/32947638/…
  • @ParaSara 在真实设备上是的
  • 我被困在这里,在这里我终于在谷歌搜索后找到了解决方案,非常感谢@JemshitIskenderov 你的回答phone settings->app->yourapp->permissions 确实帮助了我
【解决方案2】:

您在 Android 6.0+ 环境中运行此程序,并且您的 targetSdkVersion 为 23。

在这种情况下,WRITE_EXTERNAL_STORAGE 是the Android 6.0 runtime permission system 的一部分。修改您的应用以参与此系统,或将您的targetSdkVersion 降至 23 以下。

【讨论】:

  • 我未经允许测试过,没有运气
  • @JemshitIskenderov:我没有告诉你删除权限。我告诉你修改你的应用程序以参与Android 6.0运行时权限系统,或者将你的targetSdkVersion降到23以下。
  • “修改你的应用参与Android 6.0运行时权限系统”是什么意思?
  • @JemshitIskenderov:我的意思是我写的。我对the documentation on the Android 6.0 runtime permission system 的回答中有一个链接。它相当长且涉及。我的建议是你暂时将targetSdkVersion 降到 23 以下。
  • 无论是获得许可还是未经许可,它都必须工作(我都尝试过)而不是在运行时请求许可,对吗?但它没有。我不认为这是因为许可。
【解决方案3】:

您必须获得这项工作的许可,例如:

为了实现这个代码:

 if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {

        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

        } else {


            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},23
                    );
        }
    }

祝你好运!!!

【讨论】:

    【解决方案4】:

    试试这个代码。

    @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
    
                 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                if (checkPermission()) {
                                    //do your work
                                } else {
                                    requestPermission();
                                }
                            }
                   }
    
    
                    protected boolean checkPermission() {
                        int result = ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
                        if (result == PackageManager.PERMISSION_GRANTED) {
                            return true;
                        } else {
                            return false;
                        }
                    }
    
                    protected void requestPermission() {
    
                        if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                            Toast.makeText(this, "Write External Storage permission allows us to do store images. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
                        } else {
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
                            }
                        }
                    }
    
                @Override
                public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
                    switch (requestCode) {
                        case 100:
                            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                                //do your work
                            } else {
                                Log.e("value", "Permission Denied, You cannot use local drive .");
                            }
                            break;
                    }
                }
    

    【讨论】:

    • NOPE 还是不行,mkdir 不行??????在浪费了将近一个小时的时间后,我获得了所需的所有权限并得到了验证
    • 我也面临同样的问题,你找到解决方案了吗@JamisonMan111
    猜你喜欢
    • 2016-06-30
    • 2017-07-17
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    • 2015-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多