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;
}
}
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.
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);
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With