6

I'm implementing an import feature for my Android app. I wanted to let the user select files which they want to import from the external storage on their device (the downloads directory to be exact) and other online file storages such as DropBox and Google Drive.

I implemented it and tested it on an emulator and two android devices. When I tested it on the emulator, Android Version 6.0, since it doesn't have DropBox and GoogleDrive installed, I'm seeing what I'm expecting, which is being able to select a file from the Downloads directory: enter image description here

When I tested it on the Google Pixel phone, Android Version 7.1.2, everything is working as I expected. I can choose a file from the downloads directory, Google Drive as well as DropBox: enter image description here

However, when I tested it on the Samsung Galaxy 4 device, Android Version 5.0.1, I'm able to select a file from both Google Drive and Dropbox, but I don't have the option to select a file from my downloads directory: enter image description here

This is my code for selecting a file:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("text/*");
startActivityForResult(intent, GET_FILE_RESULT_CODE);

Please let me know what I can do to fix this issue. Thanks!

Charles Li
  • 1,835
  • 3
  • 24
  • 40
  • `"*/*"`........... Or. Create a download directory first? The last one is missing internal storage and recent too. And more. – greenapps May 19 '17 at 03:36

2 Answers2

4

I ended up using ACTION_OPEN_DOCUMENT which did the trick. Now I can select file from my external storage for Android 5.0.1 as well.

However, ACTION_OPEN_DOCUMENT is only supported for API 19 and above.

So this is what I did:

if (Build.VERSION.SDK_INT >= 19) {
                Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                intent.setType("text/*");
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                startActivityForResult(intent, GET_FILE_RESULT_CODE);
            }
            else {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("text/*");
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                startActivityForResult(intent, GET_FILE_RESULT_CODE);
            }

Hopefully this can help someone that runs into the same problem. Please also let me know if anyone figures out why I can't get ACTION_GET_CONTENT to select from external storage for Android 5.0.1. Thanks!

Charles Li
  • 1,835
  • 3
  • 24
  • 40
  • 1
    When I run this on KITKAT, the DocumentsActivity UI does show up, but Dropbox and other related Content providers are missing. When calling `ACTION_GET_CONTENT` they do appear on the DocumentsActivity UI, but only when I set the MIME type to "\*/\*". Please see my post below on a workaround for this. – Tjaart Dec 19 '17 at 07:32
2

To get the DocumentsActivity to appear along with other content providers such as Dropbox and ASTRO File Manager on Android versions >= KITKAT, you need to always call

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("*/*");
    intent.addCategory(Intent.CATEGORY_OPENABLE)

To set the relevant MIME type, you then have to call putExtra() with the String name Intent.EXTRA_MIME_TYPES and an array of the MIME types. On the other hand for Android versions before KITKAT, you need to define the MIME type directly when calling setType().

I could not find anything about this in the official documentation, but I have tested it on both JELLY_BEAN and KITKAT and it works.

In summary, try changing your code as follows:

if (Build.VERSION.SDK_INT >= 19) {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("*/*");
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.putExtra(Intent.EXTRA_MIME_TYPES, new String[]{"text/*"});   
    startActivityForResult(intent, GET_FILE_RESULT_CODE);
}
else {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("text/*");
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    startActivityForResult(intent, GET_FILE_RESULT_CODE);
}
Tjaart
  • 496
  • 8
  • 20