I am trying to get file path of an image from URI
, but I am getting this error
:
Caused by: java.lang.IllegalArgumentException: Invalid URI: content://media/external/images/media/9298
at android.provider.DocumentsContract.getDocumentId(DocumentsContract.java:917)
Here is the function that saves the image:
public Uri saveImage(Context context, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(context.getContentResolver(),
inImage,
DateTimeUtil.getDateTime(),
null);
return Uri.parse(path);
}
And here is the function that reads the file path of the image:
public String getFilePathFromURI(Context context, Uri uri){
String filePath = "";
String wholeID = DocumentsContract.getDocumentId(uri); <<<<<< ERROR HERE
// Split at colon, use second item in the array
String id = wholeID.split(":")[1];
String[] column = { MediaStore.Images.Media.DATA };
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
column, sel, new String[]{ id }, null);
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
return filePath;
}