0

I'm attempting to hit a web service to retrieve an image pertaining to an ID I send with the request. The web service returns a jpeg.

I have a few questions:

I want to store this jpeg in my application's SQLite3 database. What column type should I use? A blob?

What data type should I read it in as to store it in the column?

And lastly, how do I take the data from this column and throw it into an ImageView?

Andrew
  • 20,756
  • 32
  • 99
  • 177

2 Answers2

0

I would suggest against using the DB , you can just use the cache folder to store the images ans perform normal file functions on it. Here is some info : http://developer.android.com/guide/topics/data/data-storage.html#InternalCache

Ravi Vyas
  • 12,212
  • 6
  • 31
  • 47
0

Depending on the size of the image, you might want to store it instead on the SDCard (this is what Android does with images ... pictures you take, for example) and store only the Uri of that image in the DB. Small images you can store the bytes in the DB.

This is what I do, and it's worked well.

To create an image from the bytes you can use

    Bitmap theImageFromByteArray = BitmapFactory.decodeByteArray( imageByteArray, 0, imageByteArray.length );

imageView.setImageBitmap( theImageFromByteArray );
BonanzaDriver
  • 6,411
  • 5
  • 32
  • 35
  • Does using Bitmap matter if the image being sent by the web service is a jpeg? Also, because of the way I've set my web service object up, the returned jpeg is reading into a String, which I am calling getBytes() on. Is this fine? I'm getting the image back, reading to String, calling getBytes(), creating the Bitmap, and then calling setImageBitmap with the Bitmap, but nothing shows up in the ImageView. – Andrew Jun 24 '11 at 14:36
  • JPEG's are fine. Android supports those as well as PNG's natively. When you say "reading this into a String" can you clarify for me what that means exactly? If it's a case of taking the bytes of an image, and instead of ending up with a byte[] you have a String ... but the "value" of those individual bytes is the same, then you should be fine. Because, when you take that String you should be able to call imageFromString.getBytes() and the byte[] would equal the original bytes of the image back on the Server. If that's not the case then please explain. – BonanzaDriver Jun 24 '11 at 14:58
  • That is the case, but the ImageView does not populate with an image. I'm simply returning EntityUtils.toString(mResponse.getEntity()) from my web service function and calling getBytes() from that to create the Bitmap object. – Andrew Jun 24 '11 at 15:04
  • I'm really not so sure reading into a String would be the safest thing. Couldn't the image have character strings that it will read as terminators? – Andrew Jun 24 '11 at 15:43
  • Actual Bitmap images have some pretty interesting things that go into creating them (do a search on how a Bitmap is constructed and you'll see what I mean). I do not know the answer to your qustion here, but it does seem reasonable what you have proposed. Just to make sure it's not a problem with the image itself use the BitmapFactory.decodeImage() and use the overload that accepts a URL in the constructor. Point it to several websites' images and see if you can assign those images to the ImageView? If yes, the problem is either the original image, or the byte-to-string conversion. – BonanzaDriver Jun 25 '11 at 15:51