How to select a video from the gallery and get it's real path?
Source: StackOverflow & StackOverflow
Question: How can I get the real path of the selected video when the results are returned?
Answer: Here is the full code for get the video path after selecting from gallery.
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 selectedVideoUri = data.getData();
// OI FILE Manager
//filemanagerstring = selectedImageUri.getPath();
// MEDIA GALLERY
String selectedVideoPath = getPath(selectedVideoUri);
if (selectedVideoPath != null) {
Intent intent = new Intent(HomeActivity.this,
VideoplayAvtivity.class);
intent.putExtra("path", selectedVideoPath);
startActivity(intent);
}
}
}
}
public String getPath(Uri uri) {
Cursor cursor = null;
try {
String[] projection = {MediaStore.Images.Media.DATA};
cursor = getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}finally {
if (cursor != null) {
cursor.close();
}
}
}