Divya
Divya

Reputation: 182

Convert String to Bitmap

In my project i will retrieve image from mysql database and display it to Imageview. In database I have saved the link of image. So I need to convert String to Bitmap to display image. But i got error like setImageBitmap is undefined for the type of String. I am not sure what mistake i have done.

Code:

Bitmap b=StringToBitMap(Qrimage);
imgg.setImageBitmap(b);      

public Bitmap StringToBitMap(String image){
           try{
               byte [] encodeByte=Base64.decode(image,Base64.DEFAULT);

               InputStream inputStream  = new ByteArrayInputStream(encodeByte);
               Bitmap bitmap  = BitmapFactory.decodeStream(inputStream);
               return bitmap;
             }catch(Exception e){
               e.getMessage();
              return null;
             }
     }

Upvotes: 1

Views: 39646

Answers (2)

Irshad Khan
Irshad Khan

Reputation: 784

change your function

 public Bitmap StringToBitMap(String encodedString){
   try{
       byte [] encodeByte = Base64.decode(encodedString,Base64.DEFAULT);
       Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
       return bitmap;
   }
   catch(Exception e){
       e.getMessage();
       return null;
   }
 }

// second solution is you can set the path inside decodeFile function 
viewImage.setImageBitmap(BitmapFactory.decodeFile("your iamge path"));

hopefully it will work for you

Upvotes: 11

user8189050
user8189050

Reputation: 181

If you get bitmap = null, you can use:

byte[] decodedString = Base64.decode(pic, Base64.URL_SAFE );
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

Upvotes: 1

Related Questions