GIf图像在android中转换为Base64,不使用Compress

时间:2017-02-14 13:19:28

标签: android

  • 我的代码是,在这段代码中,所有图像都转换为base 64,但是在gng图像中也是png,我希望在转换后作为gif。

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

1 个答案:

答案 0 :(得分: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);
    }
}