Downloading images with webview

前端 未结 4 1344
北恋
北恋 2021-02-11 03:46

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 ?

相关标签:
4条回答
  • 2021-02-11 04:29
    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));
    }
    

    `

    0 讨论(0)
  • 2021-02-11 04:34

    Just load the URL of the image with the webview.

    0 讨论(0)
  • 2021-02-11 04:37

    This solved my problem

    `          `@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;
                }``
    

    make sure to add permissions for download manager, SD read, SD write!

    0 讨论(0)
  • 2021-02-11 04:48

    I think the best way to do it is to parse the html code of the page and get the images url.

    0 讨论(0)
提交回复
热议问题