14

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

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.

Please help me.

Thanks in advance..

1
  • What's the field type in the SQLite db for your 'image' column? Commented Jun 9, 2011 at 22:17

3 Answers 3

19
+25

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

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

    create table datatable(_id INTEGER PRIMARY KEY AUTOINCREMENT, image BLOB, price INTEGER);
Sign up to request clarification or add additional context in comments.

3 Comments

First guess is simple, column count starts with 1 and therefore column idx=2 is price, definitely not blob.
Why decodeStream() instead of decodeByteArray(b, 0, b.length);
That was not proven as root cause of the problem. I've just advised that my codepiece is working, not stating opposite statement about byteArray decoding.
2

First of all please did not store the images in database because sqlite will not support more then 1 mb,i may be wrong, images should be store in assets or drawble folder and the name of images should be store in database then you can get it from name to resource conversion by code

 String videoFileName= c.getString(c.getColumnIndex(TB_SETTING_VIDEOFILENAME));
        int dot = videoFileName.lastIndexOf(".");
        videoFileName=videoFileName.substring(0,dot);
        Log.d("VIDEOFILENAME", videoFileName);

        int videoFileRId = getResources().getIdentifier(videoFileName , "raw", getPackageName());   

I hope this will help you.

Comments

1

try this cursor object; // your cursor

   mImageView.setImageBitmap(getImageFromBLOB(object.getBlob(object.getColumnIndex("book_thumb"))));

    public static Bitmap getImageFromBLOB(byte[] mBlob)
    {
        byte[] bb = mBlob;
        return BitmapFactory.decodeByteArray(bb, 0, bb.length);
    }

1 Comment

:Hello , if you want to help me with that stackoverflow.com/questions/15954896/… . Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.