从SQLite Blob创建Drawable的问题

deS*_*lby 5 android

我将图像文件缓存为SQLite数据库中的blob.我在另一个平台上有类似的应用程序,使用相同的图像文件执行相同的操作.两个平台上的数据库报告相同图像的大小完全相同.所以我认为,但不能保证,图像数据完整地进入数据库.

但是当我尝试创建一个Drawable时,控制台打印出"DEBUG/skia(267):--- decoder-> decode返回false".

步骤是:

  1. 将blob读入字节数组.

    byte [] b = new byte [imageDataCursor.getInt(0)];

    b = imageDataCursor.getBlob(1);

  2. 从字节数组创建一个InputStream.

    ByteArrayInputStream = new ByteArrayInputStream(b);

  3. 从InputStream创建Drawable.(这是上面创建'解码器'消息的原因)

    Drawable drw = Drawable.createFromStream(is,"articleImage");

  4. 将ImageView.image设置为Drawable.

    imgView.setImageDrawable(DRW);

到目前为止,没有快乐.有什么建议?

deS*_*lby 1

我将发布我的解决方案以帮助遇到类似问题的任何人。

我尝试通过将字节数组写入文件然后查看该文件来测试从数据库读取的字节数组。事实证明,数据库中的 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);
         }
Run Code Online (Sandbox Code Playgroud)