Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oreo DocumentsContract.getDocumentId(uri) returns path instead of Long

I'm trying to get the real path of a file stored in the Android file system (I'm testing on an Emulator with Android 8.1)

here is my code:

final String id = DocumentsContract.getDocumentId(uri); final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); return getDataColumn(context, contentUri, null, null); 

For earlier versions of Android 8.0, the variable id contains a long value so the next line works as expected.

On Android 8 the variable id contains a path like this raw:/storage/emulated/0/Download/my_file.pdf, so the casting Long.valueOf(id)) throws a 'java.lang.NumberFormatException' Exception.

Any ideas? Thanks.

like image 382
Ale Avatar asked Jul 02 '18 12:07

Ale


1 Answers

Had the same problem solved it by doing the following.

final String id = DocumentsContract.getDocumentId(uri); if (!TextUtils.isEmpty(id)) {             if (id.startsWith("raw:")) {                 return id.replaceFirst("raw:", "");             }             try {                 final Uri contentUri = ContentUris.withAppendedId(                         Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));                 return getDataColumn(context, contentUri, null, null);             } catch (NumberFormatException e) {                  return null;             }       } 

Solution was found in a comment https://github.com/Yalantis/uCrop/issues/318

like image 68
Tirth Shah Avatar answered Oct 11 '22 09:10

Tirth Shah