我将图像文件作为 blob 缓存在 SQLite 数据库中。我在另一个平台上有一个类似的应用程序,它对相同的图像文件执行相同的操作。两个平台上的数据库报告相同图像的完全相同的大小。所以我认为,但不能保证图像数据完好无损地进入数据库。
但是当我尝试创建一个 Drawable 时,控制台打印出“DEBUG/skia(267): --- detector->decode returned false”。
步骤是:
-
将 blob 读入字节数组。
byte[] b = new byte[imageDataCursor.getInt(0)];
b = imageDataCursor.getBlob(1);
-
从字节数组创建一个输入流。
ByteArrayInputStream = new ByteArrayInputStream(b);
-
从输入流创建一个可绘制对象。 (这就是上面创建“解码器”消息的原因)
Drawable drw = Drawable.createFromStream(is, "articleImage");
-
将 ImageView.image 设置为 Drawable。
imgView.setImageDrawable(drw);
到目前为止,没有任何喜悦。有什么建议么?
我将发布我的解决方案以帮助遇到类似问题的任何人。
我尝试通过将字节数组写入文件然后查看该文件来测试从数据库读取的字节数组。事实证明,数据库中的 blob 数据不完整 - 我得到了图像的几行,但不是整个图像。
该解决方案涉及创建 BufferedInputStream 和 ByteArrayBuffer 以从远程服务器读取图像。从远程服务器捕获图像文件并创建字节数组以便能够将其写入 sqlite 数据库的代码如下所示:
try {
HttpGet getMethod=new HttpGet(url);
HttpClient client=new DefaultHttpClient();
HttpResponse response = client.execute(getMethod);
HttpEntity entity = response.getEntity();
InputStream in = entity.getContent();
BufferedInputStream bis = new BufferedInputStream(in);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
byte[] b = baf.toByteArray();
database.updateArticleWithImageData(b, imageKey);
}
catch (Throwable t) {
android.util.Log.e("MyApp", "Exception fetching image file", t);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)