我在webview中顯示來自移動網站的圖庫。我如何從webview下載這些圖片?是否有任何額外的設置爲webview?使用webview下載圖像
2
A
回答
2
這解決了我的問題
` `@Override
public boolean shouldOverrideUrlLoading (WebView view, String url) {
boolean shouldOverride = false;
// We only want to handle requests for image files, everything else the webview
// can handle normally
if (url.endsWith(".jpg")) {
shouldOverride = true;
Uri source = Uri.parse(url);
// Make a new request pointing to the mp3 url
DownloadManager.Request request = new DownloadManager.Request(source);
// Use the same file name for the destination
File destinationFile = new File (destinationDir, source.getLastPathSegment());
request.setDestinationUri(Uri.fromFile(destinationFile));
// Add it to the manager
manager.enqueue(request);
}
return shouldOverride;
}``
確保添加權限下載管理器,SD讀,寫SD!
2
我認爲最好的辦法是解析頁面的html代碼並獲取圖片url。
0
只需使用webview加載圖像的URL即可。
+0
我的目的是通過下載將這些圖像(顯示在網站的webview上)保存到SD卡中。 –
0
webview.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("http://m.dudamobile.com/?source=DM_DIRECT")){
DownloadManager dm = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(
Uri.parse(url));
enqueue = dm.enqueue(request);
return true;
}
else
{
view.loadUrl(url);
return true;
}
}}
//register your broadcast reciever of Download manager in the activity
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
long downloadId = intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID, 0);
Query query = new Query();
query.setFilterById(enqueue);
try{
Cursor c = dm.query(query);
if (c.moveToFirst()) {
int columnIndex = c
.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c
.getInt(columnIndex)) {
// ImageView view = (ImageView) findViewById(R.id.imageView1);
String uriString = c
.getString(c
.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
mNotificationManager.notify(1, notification);
// view.setImageURI(Uri.parse(url1));
/* Intent i = new Intent();
i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
startActivity(i);*/
}
}
}catch(NullPointerException e)
{
Toast.makeText(getApplicationContext(),"Item not downloadable :(", Toast.LENGTH_LONG).show();
}
}
}
};
registerReceiver(receiver, new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
`
相關問題
- 1. WebView - 下載javascript,css和圖像
- 2. iOS webview:提示用戶只使用JavaScript下載圖像
- 3. WebView下載圖片
- 4. WebView未使用Cookie來加載圖像
- 5. 使用JavaFX下載WebView
- 6. 使用webview android下載?
- 7. 使用AsyncTask下載圖像
- 8. 使用urllib下載圖像
- 9. 使用file_put_contents下載圖像
- 10. 使用URL下載圖像
- 11. 使用gevent下載圖像
- 12. Android Webview圖像未加載
- 13. 使用cordova.file加載下載的圖像
- 14. 試圖使用wget下載jpg圖像
- 15. 下載圖像
- 16. 使用Android Webview下載文件
- 17. 使用webview下載視頻文件
- 18. 使用Webview Windows 8下載8
- 19. 使用QML Webview下載文件
- 20. Android:使用Webview,但沒有下載
- 21. 使用NSURLSession下載大量圖像
- 22. 使用異步任務下載圖像
- 23. 使用NodeJS下載數百萬圖像
- 24. 使用android <= 2.2下載圖像
- 25. 使用PHP cURL下載多個圖像
- 26. AFNetworking圖像下載不使用AFImageRequestOperation
- 27. 如何使用AFNetworking 2.0下載圖像?
- 28. 使用curl從SSL下載圖像?
- 29. 如何使用DownloadManager下載base64圖像?
- 30. 如何使用QuickBox API下載圖像?
我的目的是通過下載將這些圖像(顯示在網站的webview上)保存到SD卡中。 –