Expected Behavior
When I am selecting the file which is stored inside "Download", it should able to retrieves its file name and path
Actual Behavior
When I am selecting the file which is stored inside "Download", it returns null.
Steps to Reproduce the Problem
- When picking file method is called, it displays the folders in Android
- Go to downloads, select a file
- It returns null in getting real path from URI
Here is the code what i implemented
public static String getPath(final Context context, final Uri uri) {
String id = DocumentsContract.getDocumentId(uri);
if (!TextUtils.isEmpty(id)) {
if (id.startsWith("raw:")) {
return id.replaceFirst("raw:", "");
}
try {
final boolean isOreo = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
String stringContentURI;
Uri contentUri;
if(isOreo){
stringContentURI = "content://downloads/my_downloads";
}else{
stringContentURI = "content://downloads/public_downloads";
}
contentUri = ContentUris.withAppendedId(
Uri.parse(stringContentURI), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
} catch (NumberFormatException e) {
return null;
}
}
}
public static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = { column};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
However, it is working when selecting file from other folders in Android device
Please advise. Thanks everyone :)