问题
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 thatUri
- 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 thefile
scheme - Any app can implement
ACTION_GET_CONTENT
forvideo/*
, and they can hand back whatever they want for aUri
- Providers other than
MediaStore
do not have to honorMediaStore.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