jigspatel
jigspatel

Reputation: 410

String to image

I use following code for get bitmap from string but i get null bitmap. So please guide me.

                    byte[]  Image_getByte = Base64.decode(img);

                    ByteArrayInputStream bytes = new ByteArrayInputStream(Image_getByte);

        BitmapDrawable bmd = new BitmapDrawable(bytes);

        Bitmap bitmap=bmd.getBitmap();

        Log.v("log","Home bitmap  "+bitmap);

        i.setImageBitmap(bitmap);

Upvotes: 0

Views: 208

Answers (4)

Java
Java

Reputation: 2489

Please consider 'ImageContents' as String which contains image data.

byte[] imageAsBytes = Base64.decode(ImageContents.getBytes());
    ImageView image = (ImageView)this.findViewById(R.id.ImageView);
    image.setImageBitmap(
            BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
    );

ImageView :In Android, you can use “android.widget.ImageView” class to display an image file

BitmapFactory:Creates Bitmap objects from various sources, including files, streams, and byte-arrays.

For more information about BitmapFactory see here

Upvotes: 0

Finder
Finder

Reputation: 1217

If img is base 64 String use the following source to get the bitmap from base 64 string.

byte[] source=img.getBytes();
byte[]  Image_getByte = Base64.decode(source);
Bitmap bitmap = BitmapFactory.decodeByteArray(Image_getByte, 0,Image_getByte.length);
imageView.setImageBitmap(bitmap);

This may help you

Upvotes: 0

Stephen C
Stephen C

Reputation: 719679

The javadoc for getBitmap() says:

"Returns the bitmap used by this drawable to render. May be null."

Upvotes: 0

Chandra Sekhar
Chandra Sekhar

Reputation: 19500

use BitmapFactory.decodeByteArray(Image_getByte)

here you don't need any String, after getting the byte array, just pass it in the mentioned method which returns Bitmap

Upvotes: 1

Related Questions