Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ACTION OPEN DOCUMENT TREE only returns empty Recent folder

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);
                }
            }
        }
like image 910
Michael Mossman Avatar asked Jan 11 '15 16:01

Michael Mossman


2 Answers

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

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.

like image 68
fthdgn Avatar answered Sep 29 '22 11:09

fthdgn


I had the same problem as you do.

I clicked "Show internal storage" in the overflow menu

enter image description here

After that I was able to see "Internal storage" in the drawer

enter image description here

like image 41
Fedor Avatar answered Sep 29 '22 09:09

Fedor