我正在创建一个应用,要求安装后直接启动时从设备存储中读取xml文件。 我遇到一个问题,在运行时询问“ Manifest.permission.READ_EXTERNAL_STORAGE”权限(并授予它)后,“ new File(Environment.getExternalStorageDirectory())。canRead()”在我重新启动应用程序之前始终返回false。 重新启动后,一切正常,但这不是我需要的。 奇怪的是,在收到授予的权限后,如果我检查应用程序是否已设置权限,一切都会很好
ContextCompat.checkSelfPermission(getContext(), Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED; -> always returns true after granting the permission
这是我的代码:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == AddServerFragment.EXTERNAL_STORAGE_READ_PREMISSION) {
if (permissions.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Logger.log(LOG_TAG, android.Manifest.permission.READ_EXTERNAL_STORAGE + " has been granted", Log.VERBOSE);
((AddServerFragment) getSupportFragmentManager().findFragmentByTag(getAttachedFragmentTag())).parseServerConfigFile();
} else {
Logger.log(LOG_TAG, android.Manifest.permission.READ_EXTERNAL_STORAGE + " has been denied", Log.INFO);
}
return;
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
ParseServerConfigFile()
void parseServerConfigFile() {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ||
Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
File serverSetupFile = new File(Environment.getExternalStorageDirectory(), getString(R.string.server_ini_file_name));
ConfigureServerAsyncTask configureServerAsyncTask = new ConfigureServerAsyncTask(this);
configureServerAsyncTask.execute(serverSetupFile);
} else {
Logger.log(LOG_TAG, "Storage not available at the moment", Log.INFO);
Toast.makeText(getContext(), "Storage not available", Toast.LENGTH_SHORT).show();
}
}
Logcat: 07-21 10:34:55.320 25272-25272 / com.mdmobile.cyclops V / LoginActivity:android.permission.READ_EXTERNAL_STORAGE已被授予 07-21 10:36:01.277 25272-25335 / com.mdmobile.cyclops V / FA:使用测量服务 连接到远程服务 07-21 10:36:01.290 25272-25335 / com.mdmobile.cyclops V / FA:活动已恢复,时间:22213849 07-21 10:36:01.291 25272-25272 / com.mdmobile.cyclops I / ConfigureServerAsyncTask:找不到ServerSetup.xml文件
有人见过吗?
答案 0 :(得分:0)
就像大卫·迈克尔(David Michael)指出的那样 可能的解决方案虽然很难看(看起来像个错误),但一旦接受许可,就可以看到应用程序重新加载:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == AddServerFragment.EXTERNAL_STORAGE_READ_PREMISSION) {
if (permissions.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Logger.log(LOG_TAG, android.Manifest.permission.READ_EXTERNAL_STORAGE + " has been granted \n app restart required",
Log.INFO);
//READ STORAGE PERMISSION requires application restart as it is a linux permission
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getBaseContext().getPackageName());
if (i != null) {
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
System.exit(0);
}
} else {
Logger.log(LOG_TAG, android.Manifest.permission.READ_EXTERNAL_STORAGE + " has been denied", Log.INFO);
}
return;
}