Set custom folder Android Download Manager

后端 未结 3 1406
梦谈多话
梦谈多话 2020-12-01 08:03

I\'ve a question about Download Manager. I\'m going to download a file from a site. When I set the default directory for download (Environment.DIRECTORY_DOWNLOAD) all works

3条回答
  •  北海茫月
    2020-12-01 08:39

    check below code: its save file in "sdcard/dhaval_files/". just replace your folder name and give permission write_external_storage in android manifest file.

    public void file_download(String uRl) {
            File direct = new File(Environment.getExternalStorageDirectory()
                    + "/dhaval_files");
    
            if (!direct.exists()) {
                direct.mkdirs();
            }
    
            DownloadManager mgr = (DownloadManager) this.getSystemService(Context.DOWNLOAD_SERVICE);
    
            Uri downloadUri = Uri.parse(uRl);
            DownloadManager.Request request = new DownloadManager.Request(
                    downloadUri);
    
            request.setAllowedNetworkTypes(
                    DownloadManager.Request.NETWORK_WIFI
                            | DownloadManager.Request.NETWORK_MOBILE)
                    .setAllowedOverRoaming(false).setTitle("Demo")
                    .setDescription("Something useful. No, really.")
                    .setDestinationInExternalPublicDir("/dhaval_files", "test.jpg");
    
            mgr.enqueue(request);
    
        }
    

提交回复
热议问题