2

パーミッションを付与したWRITE_EXTERNAL_STORAGE後、アプリの起動がOKになった後にのみ書き込むことはできません。フラグメントからリクエストしています。コードの下に問題を再現します。

Emulator 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)
4

0 に答える 0