2017-02-09 149 views
0

如何使用下載管理器下載的base64圖像(我的意思是圖像必須在應用程序下載,顯示下載(base64轉換爲字節後文件保存))如何使用DownloadManager下載base64圖像?

例如: 我需要從下一個我的自定義瀏覽器加載圖像html "<img src=\"data:image/jpeg;base64,someBase64String" />"。並加載它完全相同(用戶意見)作爲一個正常的圖像網址。對於正常的圖片url,我使用DownloadManager:

DownloadManager.Request request = new DownloadManager.Request(source); 
     request.allowScanningByMediaScanner(); 
     request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
     request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName); 
     request.setTitle(fileName); 
     request.setDescription(fileName); 
     DownloadManager dm = (DownloadManager) App.getInstance().getSystemService(Context.DOWNLOAD_SERVICE); 
     dm.enqueue(request); 

回答

0
DownloadManager dm = (DownloadManager) App.getInstance().getSystemService(Context.DOWNLOAD_SERVICE); 
     String[] fileNameAndExt = getFileNameAndExt(file.getName()); 
     dm.addCompletedDownload(fileNameAndExt[0], file.getName(), true, "image/" + fileNameAndExt[1], file.getPath(), file.length(), true); 

還需要在許可清單:

<uses-permission android:name="android.permission.INTERNET" /> 

令人奇怪的是,但對方也沒有奏效。

1

試試這個下載的圖像。 uRl是圖片網址。

public void downloadFile(String uRl) { 
     File direct = new File(Environment.getExternalStorageDirectory() 
       + "/MyBox"); 

     if (!direct.exists()) { 
      direct.mkdirs(); 
     } 

     DownloadManager mgr = (DownloadManager) getActivity().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("/MyFiles", "fileName.jpg"); 

     mgr.enqueue(request); 

    } 
+0

源代碼此類(請求)拋出異常,如果我們把不遠程uri – mlevytskiy

+0

http://grepcode.com/file/repo1.maven.org/maven2/org.robolectric/android-all/4.4_r1-robolectric -0/android/app/DownloadManager.java#DownloadManager.Request – mlevytskiy

+0

if(scheme == null ||(!scheme.equals(「http」)&&!scheme.equals(「https」))) – mlevytskiy

相關問題