web-dev-qa-db-fra.com

Définir un dossier personnalisé Android Download Manager

J'ai une question sur Download Manager. Je vais télécharger un fichier depuis un site. Lorsque j'ai défini le répertoire par défaut pour le téléchargement (Environment.DIRECTORY_DOWNLOAD), tout fonctionne bien et mon téléchargement est démarré. Mais si j'essaye de changer le répertoire, mon application ne télécharge pas le fichier. En particulier, je veux que mon fichier aille dans un dossier à l'intérieur d'un téléchargement, par exemple/storage/sdcard/Download/myFolder. Comment puis-je résoudre ce problème?

File mydownload = new File (Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+ "/myFolder");

if (!mydownload.exists()){
    mydownload.mkdir();
}

String url = sUrl[0];
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    request.allowScanningByMediaScanner();
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}

request.setDestinationInExternalPublicDir(mydownload.getAbsolutePath(),"Myfile.extension");


DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
24
bott91

vérifier le code ci-dessous: son fichier de sauvegarde dans "sdcard/dhaval_files/". remplacez simplement le nom de votre dossier et accordez l'autorisation write_external_storage dans Android.

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);

    }
88
Dhaval Parmar

Il y a deux options disponibles pour vous.

1) premier setDestinationInExternalPublicDir cela vous permettra de télécharger dans n'importe quel dossier de téléchargement standard d'androïdes basé sur le type de média, par exemple DIRECTORY_DOWNLOADS, DIRECTORY_MUSIC. ces fichiers resteront après la désinstallation.

request.setDestinationInExternalPublicDir(DIRECTORY_DOWNLOADS,
        File.separator + folderName + File.separator + fileName);

Le premier argument doit être un répertoire de téléchargements standard pour que cela fonctionne correctement et ne peut pas être autre chose.

2) le second est setDestinationInExternalFilesDir, c'est la même chose que la méthode précédente à la différence que ces fichiers seront supprimés après la désinstallation de l'application.

request.setDestinationInExternalFilesDir(context, DIRECTORY_DOWNLOADS, 
        File.separator + folderName + File.separator + fileName);

ici, le deuxième argument peut être nul ou l'un des répertoires de téléchargement Android.

11
zaphod100.10

Essayez ci-dessous le 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();
                }
1
Vinesh Chauhan