Showing posts with label Bitmap to String. Show all posts
Showing posts with label Bitmap to String. Show all posts

Friday, March 22, 2013

Convert String to Bitmap and Bitmap to String

In android, Normally we send and receive data in the form of string.So if we have image in the Bitmap form then we can not send it to server.So here i made a simple function that you need pass bitmap and it will return a string.


       public String BitMapToString(Bitmap bitmap){
            ByteArrayOutputStream ByteStream=new  ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG,100, ByteStream);
            byte [] b=ByteStream.toByteArray();
            String temp=Base64.encodeToString(b, Base64.DEFAULT);
            return temp;
      }

Here is the reverse procedure for converting string to bitmap but string should Base64 encoding


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

Mastering Jetpack Compose: Key Methods for Efficient Android UI Development

Jetpack Compose is a modern toolkit for building native Android UI. It simplifies UI development by providing a declarative approach, making...