ACTION OPEN DOCUMENT TREE仅返回空的"Recent"文件夹 [英] ACTION OPEN DOCUMENT TREE only returns empty Recent folder
问题描述
我已经仔细地复制了以前发布的以下代码段,并且该代码段在模拟器以及Nexus 9设备上都可以正常使用!
I have carefully copied the following code snippet from an earlier posting and it works, on the simulator and also on my Nexus 9 device, up to a point !
但是,我得到的只是一个空的最近"文件夹,而且我从未到达写文件的代码.
However, all I get is an empty Recent folder and I never reach the code that writes a file.
要获得适当的文档树,我必须更改什么?
What must I change to get a proper document tree ?
private void testDocumentTree() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, 42);
}
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
String TAG = "onActivityResult";
if (resultCode == RESULT_OK) {
Uri treeUri = resultData.getData();
DocumentFile pickedDir = DocumentFile.fromTreeUri(this, treeUri);
// List all existing files inside picked directory
for (DocumentFile file : pickedDir.listFiles()) {
Log.d(TAG, "Found file " + file.getName() + " with size " + file.length());
}
// Create a new file and write into it
DocumentFile newFile = pickedDir.createFile("text/plain", "My Novel");
try {
OutputStream out = getContentResolver().openOutputStream(newFile.getUri());
out.write("A long time ago...".getBytes());
out.close();
} catch (FileNotFoundException e) {
Log.d(TAG, "File Not Found, reason: ", e);
} catch (IOException e) {
Log.d(TAG,"IOException, reason: ", e);
}
}
}
推荐答案
其他人提到了DocumentsUI上的选项,用户应手动启用该选项.但是,还有另一种选择.将这些额外内容添加到您的ACTION_OPEN_DOCUMENT,ACTION_GET_CONTENT,ACTION_CREATE_DOCUMENT或ACTION_OPEN_DOCUMENT_TREE意向中.打开DocumentsUI应用程序时,存储"设置上的浏览"按钮会使用这些额外功能.我认为第一个足以显示内部存储和sdcard.其他人很好.
Others mentioned the option on DocumentsUI which the user should manually enable. However there is another option. Add these extras on to your ACTION_OPEN_DOCUMENT, ACTION_GET_CONTENT ,ACTION_CREATE_DOCUMENT or ACTION_OPEN_DOCUMENT_TREE intent. Explore button on Storage settings uses these extras while opening DocumentsUI app. I think the first one is enough to show Internal storage and sdcard. The others are good to have.
intent.putExtra("android.content.extra.SHOW_ADVANCED", true);
intent.putExtra("android.content.extra.FANCY", true);
intent.putExtra("android.content.extra.SHOW_FILESIZE", true);
Android 10
我在Android 10模拟器和OnePlus 7T上尝试过"SHOW_ADVANCED", Extra不执行任何操作.用户应手动单击以显示内部存储",然后单击显示".在三点菜单上.默认情况下,SD卡是可见的.
Android 10
I tried on Android 10 emulator and OnePlus 7T, "SHOW_ADVANCED" extra does not do anything. User should manually click to "Show Internal storage" on the three dot menu. SDCards are visible by default.
这篇关于ACTION OPEN DOCUMENT TREE仅返回空的"Recent"文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!