1

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?

KRK
  • 99
  • 11
  • Please refer [enter link description here](https://stackoverflow.com/questions/31380013/how-to-pick-image-or-video-on-android-l/31740241) – Abhishek Kumar Sep 10 '19 at 04:59
  • @AbhishekKumar Please look at the link you have provided and then look at my implementation - `intent.setAction(Intent.ACTION_GET_CONTENT);` – KRK Sep 10 '19 at 05:02

2 Answers2

0

try this code it works fine on my device

 Intent videopicker = new Intent(Intent.ACTION_GET_CONTENT);
            videopicker.setType("*/*");
            videopicker.putExtra(Intent.EXTRA_MIME_TYPES,"video/*");
            startActivityForResult(videopicker, VIDEOPICK);

onActivityResult

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        if (resultCode == RESULT_OK) {
            if (requestCode == VIDEOPICK) {
               Uri selectedvideo = data.getData();
               String videopath = selectedvideo.getPath();
                File file = new File(videopath);
                Log.e("path",file.getAbsolutePath());
            }
        }

    }catch (Exception e){

    }
}
Black mamba
  • 462
  • 4
  • 15
-1

Then If your android version is 7.0 and above you need to setup fileprovider.xml have you used it in your implementation?

  • 2
    File provider should be used when you want to share a file from your application with another application (https://developer.android.com/reference/android/support/v4/content/FileProvider) I'm selecting a file, I do not have to use a file provider. – KRK Sep 10 '19 at 05:25