2

I want to upload video selecting from gallery.

I am using Intent to select video from device:

Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Video"), PICK_VIDEO_REQUEST);

after that:

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_TAKE_GALLERY_VIDEO) {
                Uri filePath = data.getData();
                try {
                    String[] projection = {MediaStore.Video.Media.DATA};
                    Cursor cursor = getContentResolver().query(filePath, projection, null, null, null);
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(projection[0]);
                    String videoPath = cursor.getString(columnIndex);
                } catch (Exception e) {
                    Log.e("error", e.getMessage());
                }

            }
        }
    }

but when i am getting path in onActivityResult() it is returning null. I came to know that there is a change in nougat, but can't find any solution.

My code is working in all versions of android except nougat.

please anybody help.

rahul sharma
  • 152
  • 11
  • 1
    "but when i am getting path in onActivityResult() it is returning null" -- your question does not have an `onActivityResult()` method. – CommonsWare Feb 28 '17 at 12:36
  • Dude, you have to add runtime permission for reading the videos. Please make sure you have done the same . – Ashish Kumar Feb 28 '17 at 12:38
  • I have added onActivityResult() in question, – rahul sharma Feb 28 '17 at 13:05
  • @AshishSinha I already added the permissions for read and write external storage permissions – rahul sharma Feb 28 '17 at 13:06
  • Direct access is not allowed in nougat . check this [link](https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en) – Ashish M Feb 28 '17 at 13:10
  • @M.Ashish I have already seen this link, but this link just telling a way to click a image from camera and save that in .provider folder and access that file. But i want to select a video from storage and use that path to upload video. – rahul sharma Feb 28 '17 at 13:19
  • Dude i am talking about run time permissions – Ashish Kumar Feb 28 '17 at 13:31
  • @AshishSinha yes bro, if my app is running in marshmallow it means I have all required permissions at run time. the problem is because of Nougat update – rahul sharma Feb 28 '17 at 13:42
  • try this [link](http://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed) – Ashish M Feb 28 '17 at 15:13
  • @M.Ashish sorry but both links are explaining same thing, taking pics from camera. – rahul sharma Feb 28 '17 at 15:52
  • Thats not for camera only. That is the new way of accessing file. You have to create file provider which will give you the content Uri. You can't access files directly in Nougat. – Ashish M Mar 01 '17 at 04:39

2 Answers2

2


The "Android-Multipicker-Library" allows you to pick any kind of path in android particularly "Nougat"

Praveen
  • 60
  • 1
  • 7
0

My code is working in all versions of android except nougat.

Not really. It might work in a couple of scenarios on a few devices.

Your code assumes that the Uri that you get back:

  • Has the content scheme
  • Is from a provider that will return some value for MediaStore.Video.Media.DATA when queried on that Uri
  • Is one where that path is to external storage, and therefore is one that you can use

None of that is necessarily true:

  • On older devices, you are more likely to get a Uri with the file scheme
  • Any app can implement ACTION_GET_CONTENT for video/*, and they can hand back whatever they want for a Uri
  • Providers other than MediaStore do not have to honor MediaStore.Video.Media.DATA
  • The MediaStore indexes media on removable storage, whose paths are useless to you, as you have no read access to arbitrary locations on removable storage on Android 4.4+

To use a content Uri, call openInputStream() on a ContentResolver, to get an InputStream on the content. Either use the InputStream directly, or use it to make a copy of the content to some FileOutputStream on a file that you control.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491