Set custom folder Android Download Manager

后端 未结 3 1407
梦谈多话
梦谈多话 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:43

    Try Below Code:.

        String storagePath = Environment.getExternalStorageDirectory()
                            .getPath()
                            + "/Directory_name/";
                    //Log.d("Strorgae in view",""+storagePath);
                    File f = new File(storagePath);
                    if (!f.exists()) {
                        f.mkdirs();
                    }
                    //storagePath.mkdirs();
                    String pathname = f.toString();
                    if (!f.exists()) {
                        f.mkdirs();
                    }
    //                Log.d("Storage ",""+pathname);
                    dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                    Uri uri = Uri.parse(image);
                    checkImage(uri.getLastPathSegment());
                    if (!downloaded) {
                        DownloadManager.Request request = new DownloadManager.Request(uri);
                        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    
                        request.setDestinationInExternalPublicDir("/Directory_name", uri.getLastPathSegment());
                        Long referese = dm.enqueue(request);
    
                        Toast.makeText(getApplicationContext(), "Downloading...", Toast.LENGTH_SHORT).show();
                    }
    

提交回复
热议问题