我想将 webview 中显示的图像保存到本地存储中,webview 应该已经缓存了它显示的图像,我如何访问缓存的图像并将它们保存到存储中?
问问题
9906 次
4 回答
2
然后你必须为你的 WebView设置一个WebViewClient并覆盖shouldOverrideUrlLoading和onLoadResource方法。让我给你一个简单的例子:
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
}
}
}
于 2012-04-15T04:13:44.930 回答
2
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();
}
}
尝试以上从 webView 捕获图像
于 2012-04-15T03:23:46.967 回答
0
我确实使用了上面的代码并且它“工作”,但它只产生黑色图像,所以几个小时后这里是我的更正,现在它写在外部 SD 卡上,没有弃用风险或路径问题......
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();
}
}
}
这也是我的 MainActivity 的开始
public class MainActivity extends Activity {
private static final String URL = "http://punto.gt"; //your website
WebView webview;
// your code here
}
于 2013-02-01T07:14:05.770 回答
0
也许您应该检查该文件是否存在于缓存文件中。
获取 url 的哈希键。
MessageDigest md; try { md = MessageDigest.getInstance(“SHA-1”); } catch (NoSuchAlgorithmException e) { return ""; } md.update(url.getBytes()); byte b[] = md.digest();
Chromium 使用前 8 个字节的哈希码。所以也许你应该做
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)); }
然后你得到哈希字符串。chromium 中的缓存文件名是 hash + "_" + fileindex,通常文件名为零。所以文件名应该是hash_0;
2 从缓存文件中获取内容。
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;
}
于 2018-07-30T09:17:24.227 回答