2017-02-14 2 views
0
  • このコードでは、このコードではすべてのイメージがベース64に変換されますが、gifイメージでも同様に変換されます。GIFイメージはCompressを使わずにアンドロイドでBase64に変換します

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream); 
    byte[] byteArray = byteArrayOutputStream.toByteArray(); 
    return Base64.encodeToString(byteArray, Base64.NO_WRAP); 
    

答えて

0

=>このコードが正常に働いている

byte[] inputData = getBytes(iStream); 

Base64.encodeToString(inputData, Base64.NO_WRAP) 

public byte[] getBytes(InputStream inputStream) throws IOException { 
    ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(); 
    int bufferSize = 1024; 
    byte[] buffer = new byte[bufferSize]; 

    int len = 0; 
    while ((len = inputStream.read(buffer)) != -1) { 
     byteBuffer.write(buffer, 0, len); 
    } 
    return byteBuffer.toByteArray(); 
} 

public static String encodeToString(byte[] input, int flags) { 
    try { 
     return new String(encode(input, flags), "US-ASCII"); 
    } catch (UnsupportedEncodingException e) { 
     // US-ASCII is guaranteed to be available. 
     throw new AssertionError(e); 
    } 
} 
関連する問題