I'm selecting a video file from the Gallery, but in some cases data.getData()
returns null.
I open the Gallery by calling the following:
Intent intent;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
} else {
intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.INTERNAL_CONTENT_URI);
}
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("return-data", true);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(intent, SELECT_VIDEO_REQUEST);
Then in onActivityResult
I first check if data.getData()
is null or not, if it is I display a Toast
message:
if (data.getData() != null) {
//Do stuff
}else{
//Display Toast
}
I've implemented the above after seeing this blog post.
Some users reached out to me saying that they get the Toast
message a lot.
Can someone please tell me why data.getData
returns null when selecting a video file from MediaStore
?
Edit 1:
After doing more research, I found this answer. So I changed my startActivityForResult
Intent to the following:
startActivityForResult(Intent.createChooser(new Intent().
setAction(Intent.ACTION_GET_CONTENT).
setType("video/mp4"), "Select Video"),
SELECT_VIDEO_REQUEST);
I'm still not sure if this will resolve my issue because according to that answer "The protocol for ACTION_PICK requires you to supply a Uri indicating the collection you are picking from"
I'm doing it correctly. I first check if a SD Card is mounted and change the Uri accordingly android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI
.
This answer also implies the following "you will find support of ACTION_PICK spotty and inconsistent"
I would really appreciate it if someone can provide me with an informative answer why my implementation failed in some cases and if the new implementation will resolve the issue I had?