【问题标题】:Save image from webview never save to local mobile phone从 webview 保存图像永远不会保存到本地手机
【发布时间】:2017-11-17 00:36:13
【问题描述】:

我已经创建了一个代码来保存来自 webview 的图像。代码有效,但有一些问题,这里是代码

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

        final WebView.HitTestResult result = mWebView.getHitTestResult();
        if (result.getType() == WebView.HitTestResult.IMAGE_TYPE ||
                result.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {

            //menu.setHeaderTitle(result.getExtra());
            menu.add(0, 1, 0, "Save Image")
                    .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                        @Override
                        public boolean onMenuItemClick(MenuItem menuItem) {

                            String DownloadImageURL = result.getExtra();

                            if(URLUtil.isValidUrl(DownloadImageURL)){

                                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(DownloadImageURL));
                                request.allowScanningByMediaScanner();
                                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                                DownloadManager downloadManager = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
                                downloadManager.enqueue(request);

                                Toast.makeText(getContext(),"Image save successfully.",Toast.LENGTH_SHORT).show();
                            }
                            else {
                                Toast.makeText(getContext(),"Error to save image.",Toast.LENGTH_SHORT).show();
                            }
                            return false;
                        }
                    });
        }
    }

当我尝试长按图像时,它会显示对话框消息以保存图像,当我单击它时,它将显示 toast 消息,说 图像已成功保存,是的我的统计栏中的通知

问题是,当我尝试打开我的画廊并搜索图像时,我永远找不到图像,因为它没有保存到我的本地存储中。

你能帮我修复代码吗? 谢谢

【问题讨论】:

标签: android image webview save local-storage


【解决方案1】:

尝试在String DownloadImageURL = result.getExtra(); 之后从您的onMenuItemClick 调用此方法

public void saveImage(){
try {
  URL url = new URL(yourImageUrl);
  InputStream is = (InputStream) url.getContent();
  byte[] buffer = new byte[8192];
  int bytesRead;
  ByteArrayOutputStream output = new ByteArrayOutputStream();
  while ((bytesRead = is.read(buffer)) != -1) {
    output.write(buffer, 0, bytesRead);
  }
  //return output.toByteArray();
  Bitmap bm = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
} catch (MalformedURLException e) {
e.printStackTrace();
//return null;
} catch (IOException e) {
e.printStackTrace();
//return null;
}
FileOutputStream out = null;
try {
    out = new FileOutputStream(filename); //some String filename
    bm.compress(Bitmap.CompressFormat.PNG, 100, out); // bm is your Bitmap instance
    // PNG is a lossless format, the compression factor (100) is ignored
} catch (Exception e) {
    e.printStackTrace();
} finally {
    try {
        if (out != null) {
            out.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

但是,上面的方法根本没有经过测试。我搜索了一下只是因为看起来你需要更多帮助。希望这能解决您的问题。

【讨论】:

    【解决方案2】:

    测试了你的代码,我遇到了很多错误,但我已经解决了我的问题

    图库从不显示下载的图像,因为存储扫描仪从不扫描它们,实际上它们就在我们的管理器中。所以我所做的就是发送一个广播来告诉存储新图像已进入存储

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-26
      • 2013-07-18
      • 1970-01-01
      相关资源
      最近更新 更多