I have this code for decoding content://
URI:
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(contentUri,
new String[] { MediaStore.Files.FileColumns.DATA },
null, null, null);
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA);
cursor.moveToFirst();
return cursor.getString(columnIndex);
} finally {
if (cursor != null) {cursor.close()};
}
but it doesn't work for FileProvider
URI (e.g Chrome Dev URI of downloaded files: content://com.chrome.dev.FileProvider/downloads/
). Is there a way to get a real path?
You can try this solution for get real path from file provider URI.
First you need to define file provider path file as following.
provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="/storage/emulated/0" path="."/>
</paths>
Declare file provider in AndroidMenifest.xml as following.
AndroidMenifest.xml
<application>
.
.
.
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
</application>
Now in activity side.
MyActivity.java
private Uri picUri;
private void takePhotoFromCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (Utils.isNougat()) {
picUri = FileProvider.getUriForFile(this, getPackageName(), imageHelper
.createImageFile());
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}else {
picUri = Uri.fromFile(imageHelper.createImageFile());
}
intent.putExtra(MediaStore.EXTRA_OUTPUT, picUri);
startActivityForResult(intent, Const.ServiceCode.TAKE_PHOTO);
}
/**
* This method is used for handel result after captured image from camera .
*/
private void onCaptureImageResult() {
if(Utils.isNougat()){
documentImage = imageHelper.getRealPathFromURI(picUri.getPath());
}else {
documentImage = imageHelper.getRealPathFromURI(picUri);
}
}
Following my method for get real path.
public String getRealPathFromURI(Uri contentURI) {
String result;
Cursor cursor = context.getContentResolver().query(contentURI, null,
null, null, null);
if (cursor == null) { // Source is Dropbox or other similar local file
// path
result = contentURI.getPath();
} else {
cursor.moveToFirst();
try {
int idx = cursor
.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
result = cursor.getString(idx);
} catch (Exception e) {
AppLog.handleException(ImageHelper.class.getName(), e);
Toast.makeText(context, context.getResources().getString(
R.string.error_get_image), Toast.LENGTH_SHORT).show();
result = "";
}
cursor.close();
}
return result;
}
Hope you solve your problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With