I am trying to get the absolute file path from the uri. But can't get it for the files downloaded with some download manager.
For example, I have downloaded an audio and stored it in /storage/emulated/0/MIUI/.ringtone/Our Street (30-secs version)_&_7f2f552b-b5b8-41a0-94de-4b3b065b7c00.mp3. If I pick this file from Downloads section of file picker I got the following uri content://com.android.providers.downloads.documents/document/19 and I can not calculate the absolute path from this uri.
But if I pick the file from original directory i.e /storage/emulated/0/MIUI/.ringtone/ I got this uri content://com.android.externalstorage.documents/document/primary%3AMIUI%2F.ringtone%2FOur%20Street%20(30-secs%20version)_%26_7f2f552b-b5b8-41a0-94de-4b3b065b7c00.mp3 and I can get the absolute file path from this url.
How could I get the absolute file path while picked from Downloads?
I am opening file picker using following code:
Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT); chooseFile.addCategory(Intent.CATEGORY_OPENABLE); chooseFile.setType("*/*"); chooseFile.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,false); startActivityForResult(Intent.createChooser(chooseFile, "Select Audio"), PICK_AUDIO_RESULT_CODE); For parsing absolute file path from uri I'm using following code:
final String id = DocumentsContract.getDocumentId(uri); final Uri contentUri = ContentUris.withAppendedId( Uri.parse("content://downloads/public_downloads"), Long.valueOf(id) ); 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;
am trying to get the absolute file path from the uriYou should not try to do so. If you pick an uri you can use the uri as is. No need for a path. Please tell why you would.Uri.parse("content://downloads/public_downloads")Why are you hard coding? You can get all that from the picked uri to begin with.