2013-05-27 22 views
18

Ho una domanda su Download Manager. Ho intenzione di scaricare un file da un sito. Quando imposto la directory predefinita per il download (Environment.DIRECTORY_DOWNLOAD) tutto funziona correttamente e il mio download è iniziato. Ma se provo a cambiare la directory, la mia app non scarica il file. In particolare, voglio che il mio file vada in una cartella all'interno di un download, ad esempio/storage/sdcard/Download/myFolder. Come posso ripararlo?Imposta cartella personalizzata Android Download Manager

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

risposta

75

controllo sottostante Codice: la sua salvare file in "sdcard/dhaval_files/". basta sostituire il nome della cartella e dare il permesso write_external_storage nel file manifest 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); 

    } 
+0

Grazie mille! Ora funziona;) – bott91

+30

... quindi contrassegna la risposta come accettata. – Danation

+0

L'esecuzione dello stato illegale non è in grado di creare la directory –

6

Ci sono due opzioni disponibili per l'utilizzo.

1) first setDestinationInExternalPublicDir consente di scaricare in qualsiasi cartella di download standard di androids in base al tipo di file, ad esempio DIRECTORY_DOWNLOADS, DIRECTORY_MUSIC. questi file rimarranno dopo la disinstallazione.

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

Il primo argomento deve essere una directory di download standard per questo per funzionare correttamente e non può essere altrimenti.

2) il secondo è setDestinationInExternalFilesDir è lo stesso del metodo precedente con la differenza che questi file verranno eliminati dopo la disinstallazione dell'app.

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

qui il secondo argomento può essere nullo o uno qualsiasi dei androide directory di download.

0

Provare sotto il codice :.

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(); 
       } 
Problemi correlati