Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
    }
}
like image 927
John Robb Avatar asked Dec 31 '14 16:12

John Robb


3 Answers

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.

like image 65
Algorithm and Blues Avatar answered Oct 16 '22 20:10

Algorithm and Blues


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);
like image 33
George Thomas Avatar answered Oct 16 '22 19:10

George Thomas


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" />
like image 34
James Jones Avatar answered Oct 16 '22 19:10

James Jones