2016-04-30 65 views
1

我怎麼可以從我的Web服務器的某些文件在我的Android應用程序,並將它們在應用程序文件夾存放在根目錄或在內部/外部存儲器中的私人文件夾。與Android的下載管理器下載文件,並保存在應用程序文件夾

我可以下載文件,但我不能將它們存儲在私人文件夾中。

我已經寫我的下載管理器,我有一些問題,顯示它的通知(如下載百分比)。

+0

請參閱此鏈接(http://stackoverflow.com/a/3028660/3746306)。你究竟是什麼意思的私人文件夾? –

+0

這意味着其他應用程序和用戶無權編寫或讀取(或複製)文件和文件夾 – FarshidABZ

回答

2

我不能完全肯定你想用「專用文件夾」做什麼......但我這是怎麼下載的文件與進度回調:

public class Downloader extends Thread implements Runnable{ 
    private String url; 
    private String path; 
    private DownloaderCallback listener=null; 

    public Downloader(String path, String url){ 
     this.path=path; 
     this.url=url; 
    } 

    public void run(){ 
     try { 
      URL url = new URL(this.url); 
      URLConnection urlConnection = url.openConnection(); 
      urlConnection.connect(); 

      String filename = urlConnection.getHeaderField("Content-Disposition"); 
      // your filename should be in this header... adapt the next line for your case 
      filename = filename.substring(filename.indexOf("filename")+10, filename.length()-2); 

      int total = urlConnection.getContentLength(); 
      int count; 

      InputStream input = new BufferedInputStream(url.openStream()); 
      OutputStream output = new FileOutputStream(path+"/"+filename); 

      byte data[] = new byte[4096]; 
      long current = 0; 

      while ((count = input.read(data)) != -1) { 
       current += count; 
       if(listener!=null){ 
        listener.onProgress((int) ((current*100)/total)); 
       } 
       output.write(data, 0, count); 
      } 

      output.flush(); 

      output.close(); 
      input.close(); 

      if(listener!=null){ 
       listener.onFinish(); 
      } 
     } catch (Exception e) { 
      if(listener!=null) 
       listener.onError(e.getMessage()); 
     } 
    } 

    public void setDownloaderCallback(DownloaderCallback listener){ 
     this.listener=listener; 
    } 

    public interface DownloaderCallback{ 
     void onProgress(int progress); 
     void onFinish(); 
     void onError(String message); 
    } 
} 

要使用它:

Downloader dl = new Downloader("/path/to/save/file", "http://server.com/download"); 
dl.setDownloaderCallback(new DownloaderCallback{ 
    @Override 
    void onProgress(int progress){ 

    } 

    @Override 
    void onFinish(){ 

    } 

    @Override 
    void onError(String message){ 

    } 
}); 
dl.start(); 
+0

感謝您的回答,以及如何在通知中顯示下載進度?當我嘗試顯示它時,我在滾動通知欄上有一個很差的延遲 – FarshidABZ

+0

http://developer.android.com/intl/in/training/notify-user/display-progress.html –

0

對於您的私人模式,您將不得不加密文件並將其存儲在sd卡/內部。參考this

0

如果你想在外部存儲設備上下載的文件,你可以使用。

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(); 
} 
相關問題