Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ACTION_GET_CONTENT Intent not showing external storage

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!

like image 243
Charles Li Avatar asked May 19 '17 01:05

Charles Li


2 Answers

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!

like image 95
Charles Li Avatar answered Oct 30 '22 14:10

Charles Li


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);
}
like image 1
Tjaart Avatar answered Oct 30 '22 14:10

Tjaart