2016-04-30 9 views
1

ウェブサーバからアンドロイドアプリケーションでファイルをダウンロードし、ルートディレクトリのアプリケーションフォルダまたは内部/外部メモリのプライベートフォルダに保存する方法を教えてください。アンドロイドダウンロードマネージャを使用してファイルをダウンロードしてアプリケーションフォルダに保存します

ファイルをダウンロードできますが、私はそれらをプライベートフォルダに保存できません。

私は既にダウンロードマネージャーを書いており、通知の表示(ダウンロード率など)に問題があります。

+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カード/内部に保存する必要があります。 refer 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(); 
} 
関連する問題