android – 如何从图库中选择视频并获得真实路径?
作者:互联网
我可以用这段代码打开画廊,
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Video"), PICK_VIDEO_REQUEST );
它适用于三星Galaxy S5.但在某些手机中,图像会随视频一起显示.
我需要对代码进行哪些更改才能打开图库以仅显示视频?
返回结果时,如何获取所选视频的真实路径?
解决方法:
以下是从图库中选择后获取视频路径的完整代码.
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Video"),REQUEST_TAKE_GALLERY_VIDEO);
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_TAKE_GALLERY_VIDEO) {
Uri selectedImageUri = data.getData();
// OI FILE Manager
filemanagerstring = selectedImageUri.getPath();
// MEDIA GALLERY
selectedImagePath = getPath(selectedImageUri);
if (selectedImagePath != null) {
Intent intent = new Intent(HomeActivity.this,
VideoplayAvtivity.class);
intent.putExtra("path", selectedImagePath);
startActivity(intent);
}
}
}
}
// UPDATED!
public String getPath(Uri uri) {
String[] projection = { MediaStore.Video.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
if (cursor != null) {
// HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
// THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return null;
}
标签:android-gallery,android 来源: https://codeday.me/bug/20191004/1852941.html