onActivityResult Uri - 创建子文件夹并获取新子文件夹的 DocumentUri
onActivityResult Uri - create subfolder and get DocumentUri of new subfolder
我在 onActivityResult
中使用我的文件选择器的结果构建 DocumentUriUsingTree
。
现在我拿这个 DocumentUri
,建立一个 DocumentFile
来创建一个新文件夹。
onActivityResult:
if (resultCode == RESULT_OK) {
uri = data.getData();
int takeFlags = data.getFlags();
takeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION );
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O_MR1) {
getContext().getContentResolver().takePersistableUriPermission(uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION
);
}
DocumentFile subfolderDf = DocumentFile.fromTreeUri(this,uri);
subfolderDf.createDirectory("TestFolder");
uriDirFolder = uri.toString();
editor = getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit();
editor.putString(PREF_FILE_OBSERVER, uriDirFolder);
editor.apply();
}
resultUri == uriDirFolder 从 SharedPreference
get-Methode
获取
Uri docUriTree = DocumentsContract.buildDocumentUriUsingTree(Uri.parse(resultUri), DocumentsContract.getTreeDocumentId(Uri.parse(resultUri)));
docTreeUri = content://com.android.externalstorage.documents/tree/3339-6133%3AStorage%20Card%2FMusic%....
DocumentFile df = DocumentFile.fromTreeUri(this, docUriTree);
DocumentFile dirDf = df.createDirectory(dirName);
dirDf 看起来像 content://com.android.externalstorage.documents/tree/3339-6133%3AStorage%....dirName
df = DocumentFile.fromTreeUri(this,dirDf.getUri());
df returns 父文件夹而不是新创建的文件夹
我需要新文件夹中的 DocumentFileUri
才能使用 OutputStream
将文件保存到其中。
我哪里做错了?
谢谢,
亚历杭德罗
DocumentFile subfolderDf = DocumentFile.fromTreeUri(this,uri);
subfolderDf.createDirectory("TestFolder");
更改为:
DocumentFile folder = DocumentFile.fromTreeUri(this, uri);
DocumentFile subfolder = folder.createDirectory("TestFolder");
if ( subfolder == null )
{
Toast.makeText(context, "Sorry, could not create a subfolder with name TestFolder", Toast.LENGTH_LONG).show();
return;
}
Toast.makeText(context, "Created a subfolder with name TestFolder: " + subfolder.getUri().toString(), Toast.LENGTH_LONG).show();
我在 onActivityResult
中使用我的文件选择器的结果构建 DocumentUriUsingTree
。
现在我拿这个 DocumentUri
,建立一个 DocumentFile
来创建一个新文件夹。
onActivityResult:
if (resultCode == RESULT_OK) {
uri = data.getData();
int takeFlags = data.getFlags();
takeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION );
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O_MR1) {
getContext().getContentResolver().takePersistableUriPermission(uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION
);
}
DocumentFile subfolderDf = DocumentFile.fromTreeUri(this,uri);
subfolderDf.createDirectory("TestFolder");
uriDirFolder = uri.toString();
editor = getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit();
editor.putString(PREF_FILE_OBSERVER, uriDirFolder);
editor.apply();
}
resultUri == uriDirFolder 从 SharedPreference
get-Methode
Uri docUriTree = DocumentsContract.buildDocumentUriUsingTree(Uri.parse(resultUri), DocumentsContract.getTreeDocumentId(Uri.parse(resultUri)));
docTreeUri = content://com.android.externalstorage.documents/tree/3339-6133%3AStorage%20Card%2FMusic%....
DocumentFile df = DocumentFile.fromTreeUri(this, docUriTree);
DocumentFile dirDf = df.createDirectory(dirName);
dirDf 看起来像 content://com.android.externalstorage.documents/tree/3339-6133%3AStorage%....dirName
df = DocumentFile.fromTreeUri(this,dirDf.getUri());
df returns 父文件夹而不是新创建的文件夹
我需要新文件夹中的 DocumentFileUri
才能使用 OutputStream
将文件保存到其中。
我哪里做错了?
谢谢, 亚历杭德罗
DocumentFile subfolderDf = DocumentFile.fromTreeUri(this,uri); subfolderDf.createDirectory("TestFolder");
更改为:
DocumentFile folder = DocumentFile.fromTreeUri(this, uri);
DocumentFile subfolder = folder.createDirectory("TestFolder");
if ( subfolder == null )
{
Toast.makeText(context, "Sorry, could not create a subfolder with name TestFolder", Toast.LENGTH_LONG).show();
return;
}
Toast.makeText(context, "Created a subfolder with name TestFolder: " + subfolder.getUri().toString(), Toast.LENGTH_LONG).show();