由网友(ゞ糖宝宝シ°)分享简介:我使用的web视图Android中显示的图像(主要是使用谷歌AJAX API),现在,如果我想保存图片到本地存储,我该怎么办?我有图片的URL,它可用于储蓄。 I am using webview in android to display images (mainly using google ajax API),...
我使用的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);
相关推荐
最新文章