How to upload selected video from nougat, path returning null

南楼画角 提交于 2019-12-13 15:21:46

问题


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.


回答1:



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




回答2:


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.



来源:https://stackoverflow.com/questions/42508885/how-to-upload-selected-video-from-nougat-path-returning-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!