我希望用户 Select 一个文件.我是这样做的:
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
intent.setType("*/*");
startActivityForResult(intent, Aux.ACTIVITY_REQUEST_CODE_SELECT_CCRYPT_ONLY);
然后,我对活动结果执行此操作.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode,resultCode,data);
if (resultCode == RESULT_OK){
if (requestCode == Aux.ACTIVITY_REQUEST_CODE_SELECT_CCRYPT_ONLY){
Uri dataFileUri = data.getData();
Aux.DbugCcrypt("Storing the URI of the Work Directory In Shared Preferences: " + dataFileUri.toString());
Aux.storeStringInSharedPreferences(this,Aux.SHARED_PREF_STRING_KEY,dataFileUri.toString());
// And now we take persistent permission.
ContentResolver cr = this.getContentResolver();
cr.takePersistableUriPermission(dataFileUri, (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION));
}
}
else {
// TODO Handle.
Aux.DbugCcrypt("On Activity Result was not OK.");
}
}
因此,基本上我将URI字符串存储在共享首选项中,并请求对该文件的持久权限.
然后我关闭应用程序.然后再把它打开.
我这样做:
DbugCcrypt("Getting the Shared Preference URI");
String dataAuxUriString = getStringInSharedPreferences(context,SHARED_PREF_STRING_KEY);
if (!dataAuxUriString.isEmpty()){
DbugCcrypt("Got the URI File. Testing Read/Write" );
Uri dataAuxUri= Uri.parse(dataAuxUriString);
File testFile = new File(dataAuxUri.getPath());
if (testFile.isDirectory()){
DbugCcrypt("Is a directory");
}
else {
DbugCcrypt("Data Aux File URI. Can Read: " + testFile.canRead());
DbugCcrypt("Data Aux File URI. Can Write: " + testFile.canWrite());
}
}
else {
DbugCcrypt("Data Aux URI String is empty");
}
返回此打印输出(DBugCCcrypt只是调用System.err.println("[Stuf]"+Message))
[CCRYPT_DBUG] Getting the Shared Preference URI
[CCRYPT_DBUG] Got the URI File. Testing Read/Write
[CCRYPT_DBUG] Data Aux File URI. Can Read: false
[CCRYPT_DBUG] Data Aux File URI. Can Write: false
所以我的问题是,为什么我不能在应用程序执行之间获得对文件的读/写权限?