how to save images displayed in webview?

2020-07-07 07:32发布

I want to save the images displayed in webview into local storage, and webview should have cached the images it displays ,how can i access the cached images and save them into storage?

标签: android
4条回答
时光不老,我们不散
2楼-- · 2020-07-07 08:11
WebView webView = new WebView(this);
//your image is in webview

Picture picture = webView.capturePicture();
Canvas canvas = new Canvas();
picture.draw(canvas);
Bitmap image = Bitmap.createBitmap(picture.getWidth(),
picture.getHeight(),Config.ARGB_8888);
canvas.drawBitmap(mimage, 0, 0, null);
if(image != null) {
    ByteArrayOutputStream mByteArrayOS = new
    ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 90, mByteArrayOS);
    try {
        fos = openFileOutput("image.jpg", MODE_WORLD_WRITEABLE);
        fos.write(mByteArrayOS.toByteArray());
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

try the above to capture image from webView

查看更多
太酷不给撩
3楼-- · 2020-07-07 08:12

I did used the code from above and it "worked" but it was producing black images only so after a couple of hours here are my corrections, now it writes on the external sd card with no deprecation risks or path issues...

public void captureWV() {
    Picture picture = webview.capturePicture();
    Bitmap image = Bitmap.createBitmap(picture.getWidth(),picture.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(image);
    picture.draw(canvas);
    if (image != null) {
        ByteArrayOutputStream mByteArrayOS = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 90, mByteArrayOS);
        try {
            File sdCard = Environment.getExternalStorageDirectory();
            File dir = new File(sdCard.getAbsolutePath());
            File file = new File(dir, "filename.jpg");
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(mByteArrayOS.toByteArray());
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

also here is the beginning of my MainActivity

public class MainActivity extends Activity {
private static final String URL = "http://punto.gt"; //your website
WebView webview;
// your code here
}
查看更多
Deceive 欺骗
4楼-- · 2020-07-07 08:15

Then you have to set a WebViewClient to your WebView and override shouldOverrideUrlLoading and onLoadResource methods. Let me give you a simple example:

WebView yourWebView; // initialize it as always...
// this is the funny part:
yourWebView.setWebViewClient(yourWebClient);

// somewhere on your code...
WebViewClient yourWebClient = new WebViewClient(){
    // you tell the webclient you want to catch when a url is about to load
    @Override
    public boolean shouldOverrideUrlLoading(WebView  view, String  url){
        return true;
    }
    // here you execute an action when the URL you want is about to load
    @Override
    public void onLoadResource(WebView  view, String  url){
        if( url.equals("http://cnn.com") ){
            // do whatever you want
           //download the image from url and save it whereever you want
        }
    }
}
查看更多
Root(大扎)
5楼-- · 2020-07-07 08:28

Maybe you should check if the file exist in cache file.

  1. get the hash key of url.

    MessageDigest md;
    try {
        md = MessageDigest.getInstance(“SHA-1”);
    } catch (NoSuchAlgorithmException e) {
        return "";
    }
    md.update(url.getBytes());
    byte b[] = md.digest();
    

    Chromium use the pre 8 bytes of hashcode. so maybe you should do

    int i;
    StringBuffer buf = new StringBuffer("");
    for (int offset = 0; offset < 8; ++offset) {
        i = b[8 - offset - 1];
        if (i < 0)
            i += 256;
        if (i < 16)
            buf.append("0");
        buf.append(Integer.toHexString(i));
    }
    

    then you get the hash string. cache filename in chromium is hash + "_" + fileindex, usually the filename is zero. so the filename should be hash_0;

2 get the content from the cache file.

try {
    input = new FileInputStream(filename);
    FileOutputStream output = new FileOutputStream("/sdcard/img.jpg"); // save to this file
    input.skip(12); // length of key
    int len = input.read() + 12; 
    input.skip(len - 1); // skip the  key and the header
    int read;
    // magic  0xd8410d97456ffaf4;
    int flag = 0;
    while ((read = input.read()) != -1) {
          if ((flag == 0 && read == 0xd8) ||
          (flag == 1 && read == 0x41) ||
          (flag == 2 && read == 0x0d) ||
          (flag == 3 && read == 0x97) ||
          (flag == 4 && read == 0x45) ||
          (flag == 5 && read == 0x6f) ||
          (flag == 6 && read == 0xfa) ||
          (flag == 7 && read == 0xf4)) {
          flag++;
          if(flag == 8) {
              // success
              break;
          }
      } else if (flag > 0) {
          flag = 0;
      }
      output.write(read);
    }
    input.close();
    output.close();
    } catch (Exception e) {
    }
    return true;
} catch (Exception e) {
    return false;
}
查看更多
登录 后发表回答