quarta-feira, 25 de novembro de 2015

Convertendo String em Bitmap e vice versa

 public static String BitMapToString(Bitmap bitmap){  
     ByteArrayOutputStream baos=new ByteArrayOutputStream();  
     bitmap.compress(Bitmap.CompressFormat.PNG,0, baos);  
     byte [] b=baos.toByteArray();  
     String temp= Base64.encodeToString(b, Base64.DEFAULT);  
     return temp;  
   }  
   public static Bitmap StringToBitMap(String encodedString){  
     try{  
       byte [] encodeByte=Base64.decode(encodedString,Base64.DEFAULT);  
       //Bitmap bitmap= BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);  
       //return bitmap;  
       BitmapFactory.Options options=new BitmapFactory.Options();// Create object of bitmapfactory's option method for further option use  
       options.inPurgeable = true; // inPurgeable is used to free up memory while required  
       Bitmap songImage1 = BitmapFactory.decodeByteArray(encodeByte,0, encodeByte.length,options);//Decode image, "thumbnail" is the object of image file  
       Bitmap songImage = Bitmap.createScaledBitmap(songImage1, 300 , 300 , true);// convert decoded bitmap into well scalled Bitmap format.  
       return songImage;  
     }catch(Exception e){  
       e.getMessage();  
       return null;  
     }  

Nenhum comentário:

Postar um comentário