John Robb
John Robb

Reputation: 143

Files are Unselectable after calling ACTION_GET_CONTENT Intent

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;
    }
}

Upvotes: 14

Views: 7938

Answers (4)

Algorithm and Blues
Algorithm and Blues

Reputation: 893

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.

Upvotes: 15

live-love
live-love

Reputation: 52366

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
        );
    }
}

Upvotes: 0

George Thomas
George Thomas

Reputation: 4586

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);

Upvotes: 3

James Jones
James Jones

Reputation: 572

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" />

Upvotes: 7

Related Questions