在 Android 平板电脑 (3.1) 上向内存写入和读取图像时出现问题
我认为这不会太难,但在过去的几个小时里我一直在用头撞桌子,非常感谢一些帮助。本质上,我想从 url 获取图像,将其保存到内部存储器(不是 SD 卡),并能够检索该图像并稍后使用 ImageView 显示它。
这就是我从网址获取图片并将其写入内存的方式(网址存储在“图片”中):
String urlstring = pics[l][w];
if (urlstring != null){
try {
URL url = new URL(urlstring);
InputStream input = url.openStream();
FileOutputStream output = openFileOutput(("specimage"+l) + ("" +w+".jpg"), MODE_PRIVATE);
byte[] buffer = new byte[input.available()];
int n = input.read(buffer, 0, buffer.length);
while (n >= 0) {
output.write(buffer, 0, buffer.length);
n = input.read(buffer, 0, buffer.length);
}
output.close();
input.close();
} catch (Exception e) {
GlobalState.popupMessage(homePage, "Error", "Files could not be stored on disk");
}
}
这就是我尝试检索它们的方式(路径是文件名):
private Bitmap getPic(String path){
FileInputStream in;
Bitmap bMap = null;
BufferedInputStream buf;
try {
in = openFileInput(path);
buf = new BufferedInputStream(in);
byte[] bMapArray= new byte[buf.available()];
buf.read(bMapArray);
bMap = BitmapFactory.decodeStream(buf);
if (in != null) {
in.close();
}
if (buf != null) {
buf.close();
}
} catch (Exception e) {
System.out.println("excep.");
}
if (bMap == null) System.out.println("null");
return bMap;
}
如果我这样做,图片不会显示,但程序不会崩溃。不会触发异常。然而,bMap 的值被指定为 null。我还在日志中收到此奇怪的消息:
DEBUG/skia(19358): --- SkImageDecoder::Factory returned null
请让我知道我做错了什么。我绞尽脑汁也没有结果。 我应该提到我在 ui 线程中做了 setImageBitmap 。
I thought this wouldn't be too hard but have been banging my head against a desk for the last few hours and would really appreciate some help. Essentially, I want to get an image from a url, save it to internal memory (not sd card) and be able to retrieve that image and show it using an ImageView at a later time.
This is how I get the pictures from a url and write them to memory(urls are stored in "pics"):
String urlstring = pics[l][w];
if (urlstring != null){
try {
URL url = new URL(urlstring);
InputStream input = url.openStream();
FileOutputStream output = openFileOutput(("specimage"+l) + ("" +w+".jpg"), MODE_PRIVATE);
byte[] buffer = new byte[input.available()];
int n = input.read(buffer, 0, buffer.length);
while (n >= 0) {
output.write(buffer, 0, buffer.length);
n = input.read(buffer, 0, buffer.length);
}
output.close();
input.close();
} catch (Exception e) {
GlobalState.popupMessage(homePage, "Error", "Files could not be stored on disk");
}
}
This is how I attempt to retrieve them (path is the filename):
private Bitmap getPic(String path){
FileInputStream in;
Bitmap bMap = null;
BufferedInputStream buf;
try {
in = openFileInput(path);
buf = new BufferedInputStream(in);
byte[] bMapArray= new byte[buf.available()];
buf.read(bMapArray);
bMap = BitmapFactory.decodeStream(buf);
if (in != null) {
in.close();
}
if (buf != null) {
buf.close();
}
} catch (Exception e) {
System.out.println("excep.");
}
if (bMap == null) System.out.println("null");
return bMap;
}
If I do this the picture does not show up, but the program does not crash. An exception is not triggered. However, the value of bMap is given as null. I also get this strange message in the log:
DEBUG/skia(19358): --- SkImageDecoder::Factory returned null
Please let me know what I'm doing wrong. I have been ransacking my brain to no avail.
I should mention I do setImageBitmap in the ui thread.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个,(替换为你的)然后让我知道会发生什么,
Just try this, (Replace with your) and let me know what happen,