【问题标题】:Downloading images with webview使用 webview 下载图像
【发布时间】:2012-08-18 17:36:07
【问题描述】:

我正在 webview 中显示来自移动网站的画廊。如何从 webview 下载这些图像? webview 有什么额外的设置吗?

【问题讨论】:

    标签: android image webview


    【解决方案1】:

    这解决了我的问题

    `          `@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。

      【讨论】:

      • 我的目的是通过下载将这些图像(从网站显示在 webview 上)保存到 sd 卡中。
      【解决方案3】:

      只需使用 webview 加载图像的 URL。

      【讨论】:

      • 我的目的是通过下载将这些图像(从网站显示在 webview 上)保存到 sd 卡中。
      【解决方案4】:
      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));
      }
      

      `

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-05
        • 1970-01-01
        • 1970-01-01
        • 2023-04-02
        相关资源
        最近更新 更多