Displaying image from the sqlite database using cursor in android

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..

Answers


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

Need Your Help

Restrict native code functionality from Java

java c security jni sandbox

I have a java application which uses JNI in some parts to do some work. It follows the usual loading of DLL and then calling native methods of DLL. Is there any way we can restrict what native meth...

.Net IPAddress IPv4

c# .net vb.net ip-address

I have the following code:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.