ACTION OPEN DOCUMENT TREE仅返回空的Recent文件夹

Mic*_*man 12 file-io android

我已经仔细地复制了以前的帖子中的以下代码片段,它可以在模拟器上和我的Nexus 9设备上运行,直到某一点!

但是,我得到的只是一个空的Recent文件夹,我从未到达写入文件的代码.

为了获得合适的文档树,我必须改变什么?

   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);
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

fth*_*dgn 7

其他人提到了DocumentsUI上的选项,用户应手动启用该选项。但是,还有另一种选择。将这些额外内容添加到您的ACTION_OPEN_DOCUMENT,ACTION_GET_CONTENT,ACTION_CREATE_DOCUMENT或ACTION_OPEN_DOCUMENT_TREE意向中。打开DocumentsUI应用程序时,“存储”设置上的“浏览”按钮会使用这些额外功能。我认为第一个足以显示内部存储和sdcard。其他人很好。

intent.putExtra("android.content.extra.SHOW_ADVANCED", true);
intent.putExtra("android.content.extra.FANCY", true);
intent.putExtra("android.content.extra.SHOW_FILESIZE", true);
Run Code Online (Sandbox Code Playgroud)

  • 我通过阅读AOSP源代码找到了它们。我注意到,库存Android上“存储”设置上的“浏览”按钮会打开具有可见内部存储的DocumentsUI应用。就像私有API一样,因此所有设备的行为方式可能都不相同。 (2认同)

And*_*ivo 0

不确定这是否是您要问的,但在文件夹选择器中,您只能看到最近的文件夹,有一个溢出菜单,您可以在显示/隐藏 SD 卡之间切换。

溢出菜单