位图未设置为Imageview

时间:2016-10-01 11:26:18

标签: android

以下是代码:

 db.execSQL("CREATE TABLE IF NOT EXISTS SaveRetriveDB (IMG_ID INTEGER PRIMARY KEY,ImgTxt VARCHAR,StoreImg BLOB);"); 
 db.execSQL("INSERT INTO SaveRetriveDB(ImgTxt,StoreImg)VALUES('txt','" + imageInByte + "')");

ByteArrayOutputStream stream = new ByteArrayOutputStream();    
yourImage.compress(Bitmap.CompressFormat.PNG, 100, stream); 
imageInByte = stream.toByteArray(); 

Cursor c = db.rawQuery("SELECT  StoreImg FROM SaveRetriveDB where IMG_ID=2", null);
if (c.moveToNext()){
  byte InByte[] = c.getBlob(c.getColumnIndex("StoreImg"));
  Bitmap b1 = BitmapFactory.decodeByteArray(InByte, 0, InByte.length);
  ImageView imageView1=(ImageView)findViewById(R.id.imageView_image);
  imageView1.setImageBitmap(b1);           
  Toast.makeText(getApplicationContext(),InByte.toString(),Toast.LENGTH_SHORT).show();                       
}

我从DB获取字节数组,在尝试将字节数组转换为图像时,它无效。

1 个答案:

答案 0 :(得分:0)

替换您的代码
Cursor c = db.rawQuery("SELECT  StoreImg FROM SaveRetriveDB where IMG_ID=2", null);
            if (c != null && c.getCount() > 0 && c.moveToFirst())
                byte InByte[] = c.getBlob(c.getColumnIndex("StoreImg"));
                Bitmap b1 = BitmapFactory.decodeByteArray(InByte, 0, InByte.length);
                ImageView imageView1 = (ImageView) findViewById(R.id.imageView_image);
                imageView1.setImageBitmap(b1);
                Toast.makeText(getApplicationContext(), InByte.toString(), Toast.LENGTH_SHORT).show();
            }

它会起作用

快乐的编码!!