5

With Intent.ACTION_OPEN_DOCUMENT_TREE I get this uri :

content://com.android.providers.downloads.documents/tree/downloads

How can I convert it to /storage/emulated/0/Download - the real path? I don't want use

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)

because is deprecated.

EDIT : I need real path of public location for downloads, because on android Q, this is the folder where should be stored public documents.

user1063364
  • 791
  • 6
  • 21

1 Answers1

-1

Based on the docs, use DCIM/... for the RELATIVE_PATH, where ... is whatever your custom subdirectory would be. So, you would wind up with something like this:

  val resolver = context.contentResolver
  val contentValues = ContentValues().apply {
    put(MediaStore.MediaColumns.DISPLAY_NAME, "CuteKitten001")
    put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")
    put(MediaStore.MediaColumns.RELATIVE_PATH, "DCIM/PerracoLabs")
  }

  val uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)

  resolver.openOutputStream(uri).use {
    // TODO something with the stream
  }

Note that since RELATIVE_PATH is new to API Level 29, you would need to use this approach on newer devices and use getExternalStoragePublicDirectory() on older ones.

  • Maybe I don't understand your answer, but I don't see how to get real path of Downloads (what is on Android Q folder where should I store public documents) from Uri. – user1063364 Oct 08 '19 at 08:30
  • Follow this link to find Real_Path: https://developer.android.com/reference/android/provider/MediaStore.MediaColumns#RELATIVE_PATH – Shahzad Ali Oct 08 '19 at 10:51
  • Unfortunately, it does not work. resolver.query(uri, new String[]{MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.RELATIVE_PATH}, null, null, null); returns null for second field, first returns correctly display name. Maybe it works only for files, not for folders :( Tested on Q emulator – user1063364 Oct 09 '19 at 11:02
  • This answer didn't in any way answer the question at all. – yams May 18 '20 at 19:11