从SQLite数据库使用光标在android的显示图像光标、图像、数据库、SQLite

2023-09-04 23:20:39 作者:最新的八个字:幸福与我水土不服

我想在的ImageView从SQLite数据库使用光标显示图像。我用低于code检索图像,但我不能够在ImageView的显示图像。

i want to display image in imageview from sqlite database using cursor. i used below code to retrieve image but i am not able to display image in imageview.

Cursor c=this.db.query(TABLE_NAME, new String[] { "name","price","image" },
                null, null, null, null, null);
name.setText(c.getString(0));
price.setText(c.getString(1));
byte b[]=c.getBlob(2);
Bitmap bp=BitmapFactory.decodeByteArray(b, 0, b.length);
ImageView image=(ImageView)findViewById(R.id.ImageView);
//ByteArrayInputStream imageStream = new ByteArrayInputStream(b);               
//Bitmap theImage = BitmapFactory.decodeStream(imageStream);
//image.setImageBitmap(theImage);               
image.setImageBitmap(bp); 

比形象,名称和价格将显示,但图像不显示等。 任何建议或code,它会帮助我解决这个问题。

other than image, name and price will display but image will not display. any suggestion or code that will help me to solve this problem.

请帮我。

在此先感谢。

推荐答案

我试着使用,而不是字节数组流,我的例子只是为我工作。 也可以尝试使用Cursor.getColumnIndex()

I've tried to use Stream instead of byte array, mine example simply works for me. Also try to use Cursor.getColumnIndex()

    byte[] blob = c.getBlob(c.getColumnIndex(YourDB.IMAGE));
    ByteArrayInputStream inputStream = new ByteArrayInputStream(blob);
    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

下面是我的CREATE TABLE语句,请仔细检查你的

Here's my "create table" statement, pls double check yours

    create table datatable(_id INTEGER PRIMARY KEY AUTOINCREMENT, image BLOB, price INTEGER);