Android - 将 URL 中的图像保存到 SD 卡上
我想将 URL 中的图像保存到 SD 卡(以供将来使用),然后从 SD 卡加载该图像以将其用作 Google 地图的可绘制叠加层。
以下是该函数的保存部分:
//SAVE TO FILE
String filepath = Environment.getExternalStorageDirectory().getAbsolutePath();
String extraPath = "/Map-"+RowNumber+"-"+ColNumber+".png";
filepath += extraPath;
FileOutputStream fos = null;
fos = new FileOutputStream(filepath);
bmImg.compress(CompressFormat.PNG, 75, fos);
//LOAD IMAGE FROM FILE
Drawable d = Drawable.createFromPath(filepath);
return d;
图像成功保存到 SD 卡,但在到达 createFromPath()
行时失败。我不明白为什么它会保存到该目的地但不能从它加载......
I want to save an image from a URL to the SD card (for future use) and then load that image from the SD card to use it as a drawable overlay for Google maps.
Here is the save section of the function:
//SAVE TO FILE
String filepath = Environment.getExternalStorageDirectory().getAbsolutePath();
String extraPath = "/Map-"+RowNumber+"-"+ColNumber+".png";
filepath += extraPath;
FileOutputStream fos = null;
fos = new FileOutputStream(filepath);
bmImg.compress(CompressFormat.PNG, 75, fos);
//LOAD IMAGE FROM FILE
Drawable d = Drawable.createFromPath(filepath);
return d;
The image is saved to the sd card succuessfully but fails when getting to the createFromPath()
line. I don't understand why it would save ok to that destination but not load from it....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
试试这个代码。它有效...
Try this code.It works...
尝试使用此代码将图像从 URL 保存到 SDCard。
如果要在 SD 卡上创建子目录,请使用:
创建子目录“/sdcard/Wallpaper/”。
希望它能帮助你。
享受。 :)
Try this code to save the Image from the URL to SDCard.
If you want to create a sub directory on the SD card use:
To create a the sub directory "/sdcard/Wallpaper/".
Hope it will help you.
Enjoy. :)
我也遇到了同样的问题并解决了我的问题。尝试
在 AsyncTask 中声明您的网络操作,因为它将作为后台任务加载。不要在主线程上加载网络操作。
之后,在按钮单击或在内容视图中调用此类
,不要忘记添加网络权限:
希望这可以帮助某人:-)
I also faced the same issue and resolved mine with this. Try this
Declare your network operations in AsyncTask as it will load it as a background task. Don't load network operation on main thread.
After this either in button click or in content view call this class like
And don't forget to add the nework permission as:
Hope this may help someone :-)
我相信它失败了,因为您正在将位图的压缩版本写入输出流,该输出流应该使用
BitmapFactory.decodeStream()
加载。有一个 快速查看这方面的文档。如果您需要
Drawable
(decodeStream()
返回Bitmap
),只需调用Drawable d = new BitmapDrawable(bitmap).
I believe it's failing because you're writing a compressed version of the bitmap to the output stream, which should be loaded with
BitmapFactory.decodeStream()
. Have a quick look at the documentation on this.If you need a
Drawable
(decodeStream()
returns aBitmap
), simply callDrawable d = new BitmapDrawable(bitmap)
.尝试这个代码..它工作正常
并将位图保存到目录中
,并且不要忘记向清单添加权限
Try this code..It works fine
and save the bitmap into directory
and do not forget to add permission to manifest
试试这个...完成任务的简单方法。
Try this... An easy way to the task done.