2015-10-06 76 views
0

我有什麼?如何在android中點擊查看下載文件

這裏patientAttachment.getFile_path()有鏈接的網址有圖像 。


vObjPatientMedication.downloadId.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW); 
       myWebLink.setData(Uri.parse(patientAttachment.getFile_path())); 
       activity.startActivity(myWebLink); 

      } 
     }); 

現在發生的事情:我在瀏覽器中顯示的圖像

什麼,我試圖實現:我想啓動該文件的下載(文件可pdf /圖像/文字)到任何默認的位置,由android下載照顧。

如何做到這一點?

回答

2

您可以使用DownloadManager

DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); 
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(uriString)); 
downloadManager.enqueue(request); 

你也應該註冊一個接收器下載完成時就知道:

registerReceiver(onDownloadComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); 

BroadcastReceiver onDownloadComplete = new BroadcastReceiver() { 
    public void onReceive(Context ctxt, Intent intent) { 
     String action = intent.getAction(); 
     if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) { 
      //Your code 
     } 
    } 
};