Dan Hastings
Dan Hastings

Reputation: 3280

Converting BLOB image to bitmap and displaying in ImageView

I have been looking around for ages trying to get this going and i cant get the bitmap to display. I have a web service that i call with an id and it returns a BLOB image which i store in a string (img)

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http:site.com?GETBLOB&__blobRef=1&USER_ID=6379");
HttpResponse response = httpclient.execute(httpget);
BufferedReader in = new BufferedReader(new
                    InputStreamReader(response.getEntity().getContent()));
                    StringBuffer sb = new StringBuffer(""); 
                    String line = ""; 
                    String NL = System.getProperty("line.separator"); 

                    while ((line = in.readLine()) != null) 
                    { 
                        sb.append(line + NL); 
                    } 

                    in.close();
                    img = sb.toString(); 

the string img contains what appears to be a valid result when i output it to a textview so im pretty sure the code above is working fine. The problem is turning that string to a bitmap. I have tried lots of ways but from what i could find this seems the be the accepted best way to do it.

byte[] decodedString = Base64.decode(img, Base64.DEFAULT);
bm = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
ImageView imgbox = (ImageView)findViewById(R.id.imagebox);
imgbox.setImageBitmap(bm);

This is not working for me. For most methods that i have tried it just displays nothing but for this particular method it causes the app to crash on load (even though i have a try/catch.)

Upvotes: 1

Views: 6063

Answers (2)

Aye Min OO
Aye Min OO

Reputation: 25

If img is a Blob type. You can get byte[] from it.

byte[] imgByte = img.getBlob();
Bitmap bmp = BitmapFactory.decodeByteArray(imgByte,0,imgByte.length);
imgbox.setImageBitmap(bmp);

Upvotes: 0

Dan Hastings
Dan Hastings

Reputation: 3280

Seems i had over complicated it way too much. this works perfectly and also very simply

URL imgurl = new URL("http:mysite?GETBLOB&__blobRef=1&USER_ID="+ID);
InputStream picin = imgurl.openStream();
Bitmap avatar = BitmapFactory.decodeStream(picin);
imgbox.setImageBitmap(avatar);

Upvotes: 1

Related Questions