0

I'm trying to get the Download Folder to show on my file explorer. However on Android 9, when I use the getexternalstoragedirectory() method is showing self and emulated directories only and if I press "emulated" I cannot see more folders, it shows an empty folder.

So this is how I'm getting the path, it's working fine in other Android versions but Android 9. Any guide would be appreciated

val dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).absolutePath
FantomasMex
  • 219
  • 3
  • 11
  • Could the problem be in your device or emulator? I checked it and there is no problem. I can access the files and I can also see the device file explorer. – Kasım Özdemir Mar 28 '20 at 07:39

4 Answers4

0

Well, Android 9 (API 28) should not have this problem. However, as the Android docs specify, getExternalStoragePublicDirectory() has been deprecated in Android Q (ndroid 10 - API 29) you can however use MediaStore with Relative Path ( Added in API level 29 ) to access Downloads directory.

Since Relative Path is applicable only for API level 29 and above, you may want to use it for API 29 and above and use getExternalStoragePublicDirectory() for older APIs. (Assuming that you have used necessary Read/Write permissions)

So your code could be something similar to this -

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
    val resolver = context.contentResolver    
    val contentValues = ContentValues().apply {
            put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS)
          }
    val uri = resolver.insert(MediaStore.Files.getContentUri("external"), contentValues)
}else{
    val dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).absolutePath
}

You can get more details about accessing media form shared storage here - Access media files from shared storage.

Manish
  • 1,735
  • 2
  • 17
  • 30
0

On some devices and in newer Android versions Environment.getExternalStorageDirectory() no longer returns a valid path. Try using Context.getExternalFilesDir(null) instead, it should return this path: /storage/emulated/0/Android/data/your.package.name/.

Try it out to see if thats the issue. Here's the doc.

Source: From the accepted answer of this Issue writing in the Download directory

Biscuit
  • 4,840
  • 4
  • 26
  • 54
0

try this Context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)

there is a link with the answer in kotlin here

and weirdly enough the best documentation on this i found were from microsoft

quealegriamasalegre
  • 2,887
  • 1
  • 13
  • 35
0

I'm not sure of what's the use you're giving to the path. are you saving this path in shared preferences and using it later?

Make sure there is not old data saved for your app in the device, if you have, even after uninstalling and installing again the problem will be there. Make sure your path is not loaded from local data. If so, you have to clear the data manually and later set the allowbackup flag within the Manifest to FALSE. Check the official documentation for backup tag in here

Saved data paths can cause weird behaviour if the path does not exist anymore and you are trying to using it later.

Let me know if this helps.

<application
    android:name="com.name.xxx.YouApp"
    android:allowBackup="false"
Hermes
  • 467
  • 5
  • 8