I know I am asking the question which is already asked lots of time but I have tried lots of answer from them.
I have developed below code for taking pdf files and I have setup PROVIDER perfectly.
Intent intent = new Intent();
intent.setType("application/pdf");
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, PICKFILE_REQUEST_CODE);
but once I will get data on onActivityResult
then trying to get file path.
So In a few cases, I am getting file path proper. But in Samsung device, I am getting URI as content://file-content/number
. So due to this type of URI, method can't convert that URI to file path.
Here is my method which converts URI to localPath
private static String getRealPath(ContentResolver contentResolver, Uri uri, String whereClause) {
String localPath = "";
// Query the URI with the condition.
Cursor cursor = contentResolver.query(uri, null, whereClause, null, null);
if (cursor != null) {
boolean moveToFirst = cursor.moveToFirst();
if (moveToFirst) {
// Get column name by URI type.
String columnName = MediaStore.Images.Media.DATA;
if (uri == MediaStore.Images.Media.EXTERNAL_CONTENT_URI) {
columnName = MediaStore.Images.Media.DATA;
} else if (uri == MediaStore.Audio.Media.EXTERNAL_CONTENT_URI) {
columnName = MediaStore.Audio.Media.DATA;
} else if (uri == MediaStore.Video.Media.EXTERNAL_CONTENT_URI) {
columnName = MediaStore.Video.Media.DATA;
}
// Get column index.
int columnIndex = cursor.getColumnIndex(columnName);
// Get column value which is the uri related file local path.
localPath = cursor.getString(columnIndex);
}
}
return localPath;
}
So I have two question about this problem :
I want to get column name for URI but Not getting which type of URI when user select file from Drive's file.
i.e: In my method, I can't compare uri == MediaStore.Image.Media.EXTERNAL_CONTENT_URI.
How can convert
content://file-content/number
uri to file path.
Thanks in advance.