gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:41:38 24 4
gpt4 key购买 nike

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

来自 AndroidManifest.xml 的 fragment

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

构建.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)

如果在授予权限后,应用程序被终止并重新启动,则一切正常,并在外部存储中创建了备份文件。

请指出我做错了什么。

最佳答案

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

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

编辑:检查您是否已将目标 sdk 版本设置为 23。如果您已经设置并且它不起作用(或者您不想将其设置为 23),那么您可能使用此解决方案(终止应用程序进程)。

关于android - 除非在授予权限后重新启动应用程序,否则无法写入外部存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33062006/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com