除非在授予权限后重新启动应用程序,否则无法写入外部存储

时间:2015-10-11 05:51:19

标签: android android-permissions android-6.0-marshmallow

即使在运行时授予了WRITE_EXTERNAL_STORAGE,App也无法写入Android 6.0上的外部存储(我正在模拟器上测试);除非应用程序被杀死并重新启动。

来自AndroidManifest.xml的

片段

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

的build.gradle

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
    ......
    minSdkVersion 15
    targetSdkVersion 23
}

每当我需要写入外部存储器(用于备份)时,我会检查是否有权限。

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
                    getActivity().getBaseContext().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_RW_EXTERNAL_STORAGE);
                mPendingAction = PendingAction.Backup;
            } else {
                BackupRestoreService.startBackup(getActivity().getBaseContext());
            }

我也有以下

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        Log.d("SettingsActivity", "grantResultsLength: " + grantResults.length);
        if (requestCode == PERMISSION_REQUEST_RW_EXTERNAL_STORAGE) {
            Log.d("SettingsActivity", "grantResultsLength: " + grantResults.length);
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                switch (mPendingAction) {
                    case Backup:
                        BackupRestoreService.startBackup(getActivity().getBaseContext());
                        mPendingAction = PendingAction.None;
                        break;
                    case Restore:
                        break;
                    default:
                }

            } else {
                Toast.makeText(getActivity(),
                        "Permission denied",
                        Toast.LENGTH_SHORT).show();
            }
        }
    }

当用户授予权限时,以下代码

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), DIR_MY_PORTFOLIO);
if (!file.mkdirs())
        Log.d("Backup", "Unable to create directories");
final String outputFilename = new SimpleDateFormat("'Backup'-yyyyMMdd-hhmmss'.mpb'", Locale.US).format(new Date());
File outputFile = new File(getBackupStorageDir(), outputFilename);
Log.d("Backup", "Can write to file: " + outputFile.canWrite());
Log.d("Backup", "File exists: " + outputFile.exists());

产生

    in.whoopee.myportfolio D/Backup: Unable to create directories
    in.whoopee.myportfolio D/Backup: Can write to file: false
    in.whoopee.myportfolio D/Backup: File exists: false
    in.whoopee.myportfolio W/System.err: java.io.FileNotFoundException: /storage/09FD-2F0C/Download/My Portfolio/Backup-20151011-051318.mpb: open failed: EACCES (Permission denied)

如果在授予权限后,应用程序被终止并重新启动,一切都很完美,备份文件将在外部存储中创建。

请建议我做错了什么。

2 个答案:

答案 0 :(得分:3)

在成功检查权限授予后,在onRequestPermissionsResult()方法中添加以下行。

android.os.Process.killProcess(android.os.Process.myPid());

编辑:检查您是否已将目标sdk版本设置为23.如果您已经完成了该操作并且它无法正常工作(或者您不想将其设置为23)使用此解决方案(终止应用程序进程)。

答案 1 :(得分:-1)

尝试模拟新设备(例如,使用Google API的6.0 x86_64)。我有完全相同的问题,我通过在不同的模拟器上运行来解决它。