14

I am developing an app, and in this activity a button is pressed and any file can be selected to be uploaded. The file chooser loads correctly, but all of the images are unselectable (greyed out). I added the READ_EXTERNAL_STORAGE permission to the Manifest file, but I have no idea why it still won't let me choose a file. Here is the code I am using

private Button uploadButton;
private TextView uploadFile;
private static final int PICKFILE_RESULT_CODE = 1;
private String selectedImagePath;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    uploadButton = (Button)findViewById(R.id.upload_button);
    uploadFile = (TextView)findViewById(R.id.uploadFile);

    uploadButton.setOnClickListener(new View.OnClickListener()
    {

        public void onClick(View v){

            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("file/*");
            startActivityForResult(intent, PICKFILE_RESULT_CODE);
    }});
    }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(requestCode){
    case PICKFILE_RESULT_CODE:
        if (resultCode==RESULT_OK){
            String FilePath = data.getData().getPath();
            uploadFile.setText(FilePath);
        }
        break;
    }
}
John Robb
  • 143
  • 1
  • 5

4 Answers4

14

Try setting the type to "*/*" as suggested by Blundell.

If you don't want the user to be able to select content of any type you should log the file's type in onActivityResult (this answer shows how). Then try out a couple of valid files, view the log and modify intent.setType accordingly. You can use multiple types on KitKat and above as described in this answer.

7

Blundell mentioned it in the comment above, but you could also add the below to your manifest file. Also, try using setType("image/*).

    <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
James Jones
  • 572
  • 1
  • 6
  • 17
  • Blundell's recommendation worked, Thank you for the help! – John Robb Aug 06 '15 at 13:19
  • @Blundell I face the same problem. but here i read sqlite database (.mdb) file. and even i set the read permission in manifest file, i can't able to select it. but the android mobile having api level-9, it's working fine (able to select). so may i know what's wrong in api level > 9.. – Haji May 23 '16 at 10:19
3

When i tried with marshmellow and above the below code helped. I have tried with c#(xamarin). The multiple mime type is the key. Hope this helps someone

Intent intent = new Intent(Intent.ActionOpenDocument);
            intent.SetType("file/*");
            intent.AddCategory(Intent.CategoryOpenable);
            String[] mimeTypes = { "text/csv", "text/comma-separated-values" ,"application/pdf","image/*"};
            intent.PutExtra(Intent.ExtraMimeTypes, mimeTypes);
            ((FormsAppCompatActivity)Forms.Context).StartActivityForResult(intent, 7007);
George Thomas
  • 4,566
  • 5
  • 30
  • 65
  • 1
    Neverminding the lower/upper case now. But howcome you first intent.SetType("file/*"); and then intent.SetType("*/*"); The second call overwrites the first one which makes it unnecessary. – lukassos Sep 27 '17 at 21:03
  • 1
    +1 thanks :) btw I have used your solution as it is the the simplest from above – lukassos Sep 28 '17 at 03:13
0

This code works for opening text (*.txt) files. If you use ACTION_GET_CONTENT, you can also open files from google drive (or other apps that use the intent). If using ACTION_OPEN_DOCUMENT, only internal files show up. Other file types are grayed out.

Remember to have appropriate permissions in manifest file.

verifyStoragePermissions(ImportExportActivity.this);
String[] mimeTypes = { "text/plain"};
Intent intent = new Intent()
        .putExtra(Intent.EXTRA_LOCAL_ONLY, true)
        .putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes)
        .setType("file/*")
        //.setAction(Intent.ACTION_OPEN_DOCUMENT);
        .setAction(Intent.ACTION_GET_CONTENT);

startActivityForResult(Intent.createChooser(intent, "Select a text file"), 123);



private static void verifyStoragePermissions(Activity activity) {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }
}
live-love
  • 48,840
  • 22
  • 240
  • 204