gpt4 book ai didi

android - READ_EXTERNAL_STORAGE 在应用程序重新启动之前未分配

转载 作者:行者123 更新时间:2023-11-29 02:27:22 25 4
gpt4 key购买 nike

<分区>

我正在创建一个应用程序,它需要在安装后首次启动时直接从设备存储中读取 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);
}

解析服务器配置文件()

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();
}
}

日志: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: Activity 恢复,时间:2221384907-21 10:36:01.291 25272-25272/com.mdmobile.cyclops I/ConfigureServerAsyncTask:未找到 ServerSetup.xml 文件

有人见过吗?

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