授予WRITE_EXTERNAL_STORAGE权限问题?

时间:2015-12-14 13:09:10

标签: android android-fragments runtime-permissions

授予WRITE_EXTERNAL_STORAGE权限后,只有在应用启动完成后才能写入。我从片段中请求它。请使用代码复制问题。< / p>

我在模拟器6.0

中进行测试
 /**
     * Requests the WRITE_EXTERNAL_STORAGE permission.
     * If the permission has been denied previously, a SnackBar will prompt the user to grant the
     * permission, otherwise it is requested directly.
     */
    private void requestSDCardAccessPermission() {
        // BEGIN_INCLUDE(WRITE_EXTERNAL_STORAGE)
        if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
                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.
            Snackbar.make(mView, R.string.permission_sdcard_rationale,
                    Snackbar.LENGTH_INDEFINITE)
                    .setAction(R.string.ok, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            requestPermissions(
                                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                                    REQUEST_WRITE_EXTERNAL_STORAGE);
                        }
                    })
                    .show();
        } else {
            // WRITE_EXTERNAL_STORAGE has not been granted yet. Request it directly.
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    REQUEST_WRITE_EXTERNAL_STORAGE);
        }
        // END_INCLUDE(WRITE_EXTERNAL_STORAGE)
    }
  

onRequestPermissionsResult回调

       /**
         * Callback received when a permissions request has been completed.
         */
        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                               @NonNull int[] grantResults) {

            if (requestCode == REQUEST_WRITE_EXTERNAL_STORAGE) {
                // BEGIN_INCLUDE(permission_result)
                // Received permission result for WRITE_EXTERNAL_STORAGE permission.
                // Check if the only required permission has been granted
                if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // WRITE_EXTERNAL_STORAGE permission has been granted, preview can be displayed
                    Snackbar.make(mView, R.string.sdcard_available_permission,
                            Snackbar.LENGTH_SHORT).show();
                } else {
                    Snackbar.make(mView, R.string.permissions_not_granted,
                            Snackbar.LENGTH_SHORT).show();
                }
                // END_INCLUDE(permission_result)
            } else {
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            }
        }

致电

requestSDCardAccessPermission();

在清单中添加

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>;
  

更清楚的是,需要在提供后重启应用程序   运行时WRITE_EXTERNAL_STORAGE权限。

错误日志:

Unable to decode stream: java.io.FileNotFoundException: 
/storage/0E0F-380A/Pictures/2015-12-10-12-26-38-2090279097.jpg:
open failed: EACCES (Permission denied)

0 个答案:

没有答案