2011-03-28 17 views
5

¿Puedo convertir mapa de bits a cadena? Y luego, ¿vuelve a convertir el String al Bitmap?Convertir mapa de bits a cadena

Gracias de antemano.

+0

Leer esta respuesta: http://stackoverflow.com/questions/4041849/string-to-bitmap-java-android – Adnan

+0

posible duplicado de [código de Android para convertir una cadena a base 64 mapa de bits] (http://stackoverflow.com/questions/3801760/android-code-to-convert-base64-string-to-bitmap) –

+0

posible duplicado de http://stackoverflow.com/questions/4837110/how-to -convert-a-base64-string-a-bitmap-image-to-show-it-in-a-imageview –

Respuesta

0

cualquier formato de archivo

public String photoEncode(File file){ 
     try{ 

      byte[] byteArray = null; 

      BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file)); 
      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      byte[] b = new byte[1024*8]; 
      int bytesRead =0; 

      while ((bytesRead = bufferedInputStream.read(b)) != -1) 
      { 
       bos.write(b, 0, bytesRead); 
      } 

      byteArray = bos.toByteArray(); 
      bos.flush(); 
      bos.close(); 
      bos = null; 

      return changeBytetoHexString(byteArray); 

     }catch(Exception ex){   
      ex.printStackTrace(); 
      return null; 
     } 
    } 

    private String changeBytetoHexString(byte[] buf){ 


     char[] chars = new char[2 * buf.length]; 
     for (int i = 0; i < buf.length; ++i) 
     { 
      chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4]; 
      chars[2 * i + 1] = HEX_CHARS[buf[i] & 0x0F]; 
     } 
     return new String(chars); 
    } 
Cuestiones relacionadas