保存图像,web视图,机器人 [英] Saving image, webview, android
本文介绍了保存图像,web视图,机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的web视图Android中显示的图像(主要是使用谷歌AJAX API),现在,如果我想保存图片到本地存储,我该怎么办?我有图片的URL,它可用于储蓄。
I am using webview in android to display images (mainly using google ajax API), Now if I want to save an image into local storage, How do I do ? I have image url, which can be used for saving.
推荐答案
如果你有图片的URL,这是容易死人。你只需要检索图像的字节数。这里是一个示例,应该可以帮助您:
If you have the image url, this is dead easy. You just have to retrieve the bytes of the image. Here is a sample that should help you :
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();
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
这将返回任何你喜欢的ByteArray,你既可以存储或重用创建一个图像,这样做的:
This will return a byteArray that you can either store wherever you like, or reuse to create an image, by doing that :
Bitmap bm = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
这篇关于保存图像,web视图,机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文