【问题标题】:cant load the image from sqlite database in android无法从android中的sqlite数据库加载图像
【发布时间】:2013-08-03 06:20:18
【问题描述】:

我尝试从数据库加载图像,但图像未加载,logcat 显示 null 是不是和我编程的方式一样。。

imgdisplay = (ImageView) findViewById(R.id.imgIcon);
myDB = this.openOrCreateDatabase("hello", MODE_PRIVATE, null);

Cursor c = myDB.rawQuery(
    "SELECT image  FROM employee_details WHERE name= 'vv'", null);

if (c.getCount() > 0) {
    c.moveToFirst();
    do {
        byte[] blob = c.getBlob(c.getColumnIndex("image"));
        ImageView iv = (ImageView) findViewById(R.id.imgIcon);
        iv.setImageBitmap(BitmapFactory.decodeByteArray(blob, 0,blob.length));
    } while (c.moveToNext());
}
c.close();
myDB.close();

Logcat 错误是...

08-02 04:51:25.471: D/skia(8433): --- SkImageDecoder::Factory returned null

【问题讨论】:

  • 你检查过你的表返回字节数组了吗??
  • @ArmaanStranger 你能给我代码吗????
  • 检查您的数据库伙伴。名称“vv”的“图像”列中是否有任何图像数据。
  • @ArmaanStranger 是的.. 它显示 [B@40d12280
  • 只显示这么多字符串???

标签: android database sqlite blob


【解决方案1】:

试试下面的代码..

byte[] Image_bytes = cursor.getBlob(cursor.getColumnIndex("image"));
ImageView iv = (ImageView) findViewById(R.id.imgIcon);
iv.setImageBitmap(new ImageConversation().convertArrayToBmp(Image_bytes));

...

public Bitmap convertArrayToBmp(byte[] array) {
    Bitmap bitmap = BitmapFactory.decodeByteArray(array, 0, array.length);
    return bitmap ;
}

使用以下循环

c.moveToFirst();
while(!c.isAfterLast()) {
    byte[] blob = c.getBlob(c.getColumnIndex("image"));
    ImageView iv = (ImageView) findViewById(R.id.imgIcon);
    iv.setImageBitmap(BitmapFactory.decodeByteArray(blob, 0,blob.length));
    c.moveToNext()
}

【讨论】:

  • 它在 ImageConversation() 上显示错误,显示要创建一个新类
  • 检查你的 do while 循环......也许它永远不会结束。
【解决方案2】:

尝试做这样的事情 -

如果您将图像作为字符串存储在数据库中

 if(image.isEmpty())
  {
   System.out.println("is empty image");

  }
  else
  {
   byte[] decodedByte = Base64.decode(image, 0);
   Bitmap bm = BitmapFactory.decodeByteArray(decodedByte, 0,
     decodedByte.length);
   iv.setImageBitmap(bm);
  }

【讨论】:

    【解决方案3】:

    我没有找到将图像保存到数据库的方法,所以我开始将图像直接保存到内部存储器,这不是问题的答案,但可能会给其他同样不知道的人一些想法有什么想法……

    要保存到内存中...

       File fileWithinMyDir = getApplicationContext().getFilesDir();  
        try 
              {
                  StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                  .permitAll().build();
              StrictMode.setThreadPolicy(policy); 
               URL url = new URL("http://t2.gstatic.com  /images?q=tbn:ANd9GcQjZgUffqqe2mKKb5VOrDNd-ZxD7sJOU7WAHlFAy6PLbtXpyQZYdw");
               File file = new File( fileWithinMyDir.getAbsolutePath()  + "/" +"sun.jpg");
               URLConnection ucon = url.openConnection();
               InputStream is = ucon.getInputStream();
               BufferedInputStream bis = new BufferedInputStream(is);
               ByteArrayBuffer baf = new ByteArrayBuffer(50);
               int current = 0;
               while ((current = bis.read()) != -1) 
               {
                baf.append((byte) current);
               }
    
               FileOutputStream fos = new FileOutputStream(file);
               fos.write(baf.toByteArray());
               fos.close();
            } 
            catch (IOException e) 
            {
                Log.e("download", e.getMessage());
            }
    

    从内存中加载图像..

     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
              .permitAll().build();
               StrictMode.setThreadPolicy(policy); 
              mImgView1 = (ImageView) findViewById(R.id.mImgView1); 
    
              Bitmap bitmap = BitmapFactory.decodeFile(fileWithinMyDir.getAbsolutePath()  + "/" +"sunn"+".file extension");
              mImgView1.setImageBitmap(bitmap);
    

    这适用于由于“android.os.networkonmainthreadexception”而显示错误的较新的android

    如果你想解决问题,你也可以使用 AsyncTask...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-10
      • 1970-01-01
      • 1970-01-01
      • 2014-11-12
      • 1970-01-01
      • 2013-08-02
      • 1970-01-01
      • 2012-01-19
      相关资源
      最近更新 更多