Reputation: 1225
I want to retrieve real path of video from URI
I open video gallery with startActivityForResult
startActivityForResult(
Intent.createChooser(
new Intent(Intent.ACTION_GET_CONTENT)
.setType("video/*"), "Choose Video"),
REQUEST_VIDEO
);
and I am receiving it in onActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
if(requestCode == REQUEST_VIDEO){
Uri uri = data.getData();
String realpath = getRealPathFromURI(getContext(),uri);
}
}
In getRealPathFromUri
is like this;
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, 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();
}
}
}
But realpath always return null;
I search for that but still returns null
It currents with sdk_version?
or is it changed for MediaStore.Images.Media.DATA
to MediaStore.Video.Media.DATA
?
I am stuck here, having no clue, kindly suggest! Any help will be very valuable
--Thanks!
Upvotes: 3
Views: 4141
Reputation: 191
I guess you have not granted reading and writing to external storage permission. I have the same problem and granting the permission didn't solve it. Who knows, may it solves yours!
Upvotes: 0
Reputation: 1349
As far as I could understand, the way you get "real" file path depends on the Android's SDK version.
Scenario 1) I was running targetSdkVersion 28 on Emulator and I can get the "real" file path with this simple command in onActivityResult
:
data.getData().getPath();
Scenario 2) But in my physical device SDK level 25, I get exactly what you showed: a content path or something like this.
content://media/external/file/3131
Where the "real" file path was:
/storage/emulated/0/Download/lista_produtos.xlsx
In order to get the "real" file path in my physical device and also take into account my emulator, I used this piece of code inside the onActivityResult
:
Uri uri = data.getData();
String realPath;
if (DocumentsContract.isDocumentUri(getApplicationContext(), uri)) {
String[] path = uri.getPath().split(":");
realPath = path[1];
Log.i("debinf ProdAct", "Real file path on Emulator: "+realPath);
} else {
CursorLoader cursorLoader = new CursorLoader(getApplicationContext(), uri, null, null, null,null);
Cursor cursor = cursorLoader.loadInBackground();
int idx = cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA);
cursor.moveToFirst();
realPath = cursor.getString(idx);
Log.i("debinf ProdAct", "Real file path in physical device "+realPath);
cursor.close();
}
I guess in your case, the more appropiated index is:
int idx = cursor.getColumnIndex(MediaStore.Video.Media.DATA);
I hope it helps, because I have not used your code. And as I said before, it depends on the Android's SDK version.
See also: https://stackoverflow.com/a/49687681/4300670
Upvotes: 1
Reputation: 6114
Once you have the Uri
, you can obtain the absolute file path this way:
File myFile = new File(uri.getPath());
myFile.getAbsolutePath();
Upvotes: 0
Reputation: 82
Try this,
public static String getRealVideoPathFromURI(ContentResolver contentResolver,
Uri contentURI) {
Cursor cursor = contentResolver.query(contentURI, null, null, null,
null);
if (cursor == null)
return contentURI.getPath();
else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(Video.VideoColumns.DATA);
try {
return cursor.getString(idx);
} catch (Exception exception) {
return null;
}
}
}
Upvotes: 1