1

I am using downloadManager to download large files from server, and I wanted to store these files in my removable storage like SD card or USB(pendrive). My download location is /storage/511B-14E9/Media . I can successfully created the Media folder in my removable storage. But the download files are not saved in this location.

Permissions in Manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

android:requestLegacyExternalStorage="true"

Method to find USB

 fun getUSB(): String? {
        val storageDirectory = File("/storage")
        if (!storageDirectory.exists()) {
            Log.e(
                "ZZZZ",
                "getUSB: '/storage' does not exist on this device"
            )
            return ""
        }
        val files = storageDirectory.listFiles()
        if (files == null) {
            Log.e(
                "ZZZZ",
                "getUSB: Null when requesting directories inside '/storage'"
            )
            return ""
        }
        val possibleUSBStorageMounts: ArrayList<String> = ArrayList()
        for (file in files) {
            val path = file.path
            if (path.contains("emulated") ||
                path.contains("sdcard") ||
                path.contains("self")
            ) {
                Log.d("ZZZZ", "getUSB: Found '$path' - not USB")
            } else {
                possibleUSBStorageMounts.add(path)
            }
        }
        if (possibleUSBStorageMounts.size == 0) {
            setErrorLog(44,"Did not find any possible USB mounts")
            return ""
        }
        if (possibleUSBStorageMounts.size > 1) {
            Log.d(
                "ZZZZ",
                "getUSB: Found multiple possible USB mount points, choosing the first one"
            )
        }
        isUsbFound = possibleUSBStorageMounts.size != 0
        if (PreferenceManager.getDefaultSharedPreferences(this)
                .getString("pref_usb", "") == "" || !possibleUSBStorageMounts.contains(
                PreferenceManager.getDefaultSharedPreferences(this)
                    .getString("pref_usb", "")
            )
        ) {

            PreferenceManager.getDefaultSharedPreferences(this)
                .edit()
                .putString("pref_usb", possibleUSBStorageMounts[0])
                .apply()
        } else if (possibleUSBStorageMounts.size > 1) {
            possibleUSBStorageMounts.forEach { usb ->
                if (usb == PreferenceManager.getDefaultSharedPreferences(this)
                        .getString("pref_usb", "")
                ) {
                    possibleUSBStorageMounts[0] = usb
                }
            }
        }
        return possibleUSBStorageMounts[0]
    }

Method for downloading videos

private fun downloadFromUrl(url : String){

   if (manager == null) {
                manager =
                    getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
            }

            if (url != "") {
                val request = DownloadManager.Request(Uri.parse(url))
                request.setDescription("")
                request.setTitle("Download")
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)

 val path = getUSB() + "/"
                        val dir = File(path, "Media")
                        if (!dir.exists()) {
                            dir.mkdir()
                        }

                        val file = File(dir, url.md5() + ".mp4")
                        val uri = Uri.fromFile(file)
                        request.setDestinationUri(uri)
}

My quest is, is it possible to download files directly to removable storage ? . If yes how it is?

Note : Now I can download files in Mi Android TV, but it is not possible in Sony Bravia Android TV(Access denied, can't write anything to usb).

Thanks in advance.

  • `request.setDestinationInExternalFilesDir` Well getExternalFilesDir() has nothing to do with a removable micro sd card. And what is that path parameter? – blackapps Apr 30 '21 at 07:09
  • Use request.setDestinationUri() or however it is called. You can use uri from file. Tell Android version of used device. – blackapps Apr 30 '21 at 07:10
  • path = /storage/511B-14E9/ Working with Mi-Android TV, version - Android 9 – Akhil Soman Apr 30 '21 at 07:17
  • And... Did you try... Does it work now? – blackapps Apr 30 '21 at 08:32
  • yes, but its work for my Mi Android TV, but Sony Bravia Android TV it is not working, trying to access usb it says "Access was denied" error. why? – Akhil Soman May 13 '21 at 05:51
  • Please post full code as it is unclear what you do. – blackapps May 13 '21 at 06:32
  • My requirement is that, download videos from server by using android download manager and the download location should be the connected usb(pendrive). This feature is working in Mi Android TV. But Not working in Sony Bravia Android TV. Trying to create file in a usb drive connected to sony TV, it say access was denied. – Akhil Soman May 13 '21 at 07:11
  • ????????????????? `Please post full code as it is unclear what you do. ` – blackapps May 13 '21 at 07:16
  • @blackapps : Pls check – Akhil Soman May 13 '21 at 07:32
  • `if (!dir.exists()) { dir.mkdir() }` Better: `if (!dir.exists()) { if (!dir.mkdir()) return; }` Display a Toast() too to inform the user about the failure. In general you do not have to create the intended directories as DownloadManager will create them on the fly. Further a lot depends on the Android version of the used device. – blackapps May 13 '21 at 07:37
  • `val files = storageDirectory.listFiles()` On an Android 11 device the /storage directory is not listable. And on Android 10 usb-otg will often not be listed. – blackapps May 13 '21 at 07:40
  • `possibleUSBStorageMounts` You will find a removable micro sd card too. – blackapps May 13 '21 at 07:41
  • `Android Download Manger is not working in removable storage` The subject does not cover your problem as DownloadManager can download to a removable micro sd card. Dont know for a usb otg drive. Please report for both separately. And mention Android version too. – blackapps May 13 '21 at 07:46
  • `if (!dir.mkdir()) return; ` Probably if the medium is not writable for your app then it still can be writable for the DownloadManager. So maybe its better to continue. – blackapps May 13 '21 at 07:50
  • But, This is working in Android Mi TV, both Sony And Mi Android TV has same version 9 – Akhil Soman May 13 '21 at 08:28
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/232331/discussion-between-akhil-soman-and-blackapps). – Akhil Soman May 13 '21 at 08:34

0 Answers0