无法将图像从SQLite存储到ImageView

时间:2017-06-17 18:57:19

标签: android image sqlite

我有这个恼人的问题。我试图将图像从可绘制文件夹存储到数据库然后检索它并在ImageView中设置它,但当我尝试将其存储在ImageView并运行它时我没有改变我放在首位的默认图片,ImageView已经消失了。 以下是ImageView所在活动的代码和数据库处理程序的代码。 MainActivity

    protected void onCreate(Bundle savedInstanceState)  {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    ImageView img = (ImageView) findViewById(R.id.maintestImage);
    images();
    img.setImageBitmap(dbHandler.getImageDiet());



    initNewProf();
    initExercises();
}
public void images(){ //get the image from the drawable and store it in the base
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Bitmap bitmap = ((BitmapDrawable)getResources().getDrawable(R.drawable.abs)).getBitmap();

    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    byte[] photo = baos.toByteArray();
        dbHandler.saveDiet(photo);
}` DB handler
 public Bitmap getImageDiet(){ //retrive the image from the base
    SQLiteDatabase db = getReadableDatabase();
    Cursor c =  db.rawQuery("SELECT imagedata FROM "+ TABLE_DIET, null);
    byte[] photo = hexStringToByteArray("e04fd020ea3a6910a2d808002b30309d");
    while(c.moveToNext())
    {
          photo=c.getBlob(3);
    }
    ByteArrayInputStream imageStream = new ByteArrayInputStream(photo);
    Bitmap theImage = BitmapFactory.decodeStream(imageStream);
    imgView.setImageBitmap(theImage);

}

0 个答案:

没有答案