Downloading Images With Webview
I m displaying a gallery from a mobile website in webview . How can i download those images from webview ? Are there any extra settings for webview ?
Solution 1:
This solved my problem
` `@OverridepublicbooleanshouldOverrideUrlLoading(WebView view, String url) {
booleanshouldOverride=false;
// We only want to handle requests for image files, everything else the webview// can handle normallyif (url.endsWith(".jpg")) {
shouldOverride = true;
Urisource= Uri.parse(url);
// Make a new request pointing to the mp3 url
DownloadManager.Requestrequest=newDownloadManager.Request(source);
// Use the same file name for the destinationFiledestinationFile=newFile (destinationDir, source.getLastPathSegment());
request.setDestinationUri(Uri.fromFile(destinationFile));
// Add it to the manager
manager.enqueue(request);
}
return shouldOverride;
}``
make sure to add permissions for download manager, SD read, SD write!
Solution 2:
Solution 3:
Just load the URL of the image with the webview.
Solution 4:
webview.setWebViewClient(newWebViewClient(){
publicbooleanshouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("http://m.dudamobile.com/?source=DM_DIRECT") ){
DownloadManagerdm= (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
Requestrequest=newRequest(
Uri.parse(url));
enqueue = dm.enqueue(request);
returntrue;
}
else
{
view.loadUrl(url);
returntrue;
}
}}
//register your broadcast reciever of Download manager in the activityBroadcastReceiverreceiver=newBroadcastReceiver() {
@OverridepublicvoidonReceive(Context context, Intent intent) {
Stringaction= intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
longdownloadId= intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID, 0);
Queryquery=newQuery();
query.setFilterById(enqueue);
try{
Cursorc= dm.query(query);
if (c.moveToFirst()) {
intcolumnIndex= c
.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c
.getInt(columnIndex)) {
// ImageView view = (ImageView) findViewById(R.id.imageView1);StringuriString= 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, newIntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
`
Post a Comment for "Downloading Images With Webview"