1

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; 
7
  • am trying to get the absolute file path from the uri You 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. Commented May 8, 2021 at 11:51
  • Uri.parse("content://downloads/public_downloads") Why are you hard coding? You can get all that from the picked uri to begin with. Commented May 8, 2021 at 11:52
  • I need the absolute path to execute some command using ffmpeg Commented May 8, 2021 at 11:54
  • Well that provider will not give you a path in .DATA column. Only .DISPLAY_NAME and such. Maybe on an old Android version but not for instance on Android Q. Commented May 8, 2021 at 11:59
  • any suggestion for how to get things working? Commented May 8, 2021 at 12:01

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.