问题从SQLite的斑点创造一个可绘制 [英] Problem creating a Drawable from a SQLite Blob

查看:109
本文介绍了问题从SQLite的斑点创造一个可绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我缓存图像文件作为一个SQLite数据库中的BLOB。我在做同样的事情,与同一图像文件的另一个平台的类似应用程序。在两个平台上的数据库报告完全相同的尺寸相同的图像。所以我想,但不能保证,该图像数据进入数据库的完整。

I'm caching image files as a blob in a SQLite database. I have a similar application on another platform that does the same thing with the same image files. The databases on both platforms report the exact same size for the same images. So I think, but can't guarantee, that the image data is getting into the database intact.

但是,当我尝试创建一个可绘制,控制台会打印出来。DEBUG / Skia的(267):---德codeR->德code返回false

But when I try to create a Drawable, the console prints out "DEBUG/skia(267): --- decoder->decode returned false".

的步骤是:

  1. 读取BLOB到一个字节数组中。

  1. Read the blob into a byte array.

字节[] B =新的字节[imageDataCursor.getInt(0)];

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

B = imageDataCursor.getBlob(1);

b = imageDataCursor.getBlob(1);

创建从字节数组的InputStream。

Create an InputStream from the byte array.

ByteArrayInputStream的是=新ByteArrayInputStream的(B);

ByteArrayInputStream is = new ByteArrayInputStream(b);

创建从InputStream一个绘制对象。 (这是什么造成了去codeR上述消息)

Create a Drawable from the InputStream. (this is what creates the 'decoder' message above)

绘制对象DRW = Drawable.createFromStream(就是articleImage);

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

在ImageView.image设置为绘制对象。

Set the ImageView.image to the Drawable.

imgView.setImageDrawable(DRW);

imgView.setImageDrawable(drw);

到目前为止,没有喜悦。有什么建议?

So far, no joy. Any suggestions?

推荐答案

我会后我的解决方案,以帮助任何人有类似的问题。

I'll post my solution to help anyone with a similar problem.

我试图将其写入一个文件,然后查看文件测试字节数组,我从数据库中读取。事实证明,在数据库中的blob数据是不完整的。 - 我得到的图像的几行,而不是整个图像

I tried testing the byte array I'm reading from the database by writing it to a file and then viewing the file. It turns out that the blob data in the database is not complete - I get a few lines of the image, but not the entire image.

该解决方案参与创建的BufferedInputStream和ByteArrayBuffer从远程服务器读取的图像中。在code从远程服务器捕获的图像文件,并创建一个字节数组,能够将其写入到一个SQLite数据库是这样的:

The solution involved creating a BufferedInputStream and a ByteArrayBuffer to read in the image from the remote server. The code to capture an image file from a remote server and create a byte array to be able to write it to a sqlite database looks like this:

             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);
         }

这篇关于问题从SQLite的斑点创造一个可绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆