从Base64和GZIP解压缩位图

时间:2014-07-01 15:32:55

标签: java android base64 gzip

在我的应用程序中,我为用户获取了一张个人资料图片,然后将其保存为序列化类作为字符串。

我使用下面的代码执行GZIP压缩和Base64,但是我不能像你在getProfilePicture()方法中看到的那样做相反的事情:

private void saveBitmap(){
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    mBitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
    byte[] byteArray = byteArrayOutputStream.toByteArray();
    String bitmapContent = "";
    try {
        bitmapContent = FileHelpers.compressAndBase64(byteArray);
        //later save it...
        } catch (IOException e) {
          e.printStackTrace();
          Log.e(TAG, "Error converting bitmap to gzip and base64");
         }
}


public static String compressAndBase64(byte[] byteArray)
        throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream zos = new GZIPOutputStream(baos);
    zos.write(byteArray);
    zos.close();
    byte[] bytes = baos.toByteArray();
    return Base64.encodeToString(bytes, Base64.DEFAULT);
}

现在我想将它转换回Bitmap ...但到目前为止我还没有成功。

这些步骤将字符串从Base64解码回字节数组,然后解压缩字节数组并转换为Bitmap。

   public Bitmap getProfilePicture(){

        if(getProfilePhotoBase64() != null) {
            byte[] decoded = Base64.decode(mProfilePhotoBase64.getBytes(), Base64.DEFAULT);
            final int BUFFER_SIZE = 32;
            ByteArrayInputStream is = new ByteArrayInputStream(decoded);
            GZIPInputStream gis = null;
            try {
                gis = new GZIPInputStream(is, BUFFER_SIZE);
            } catch (IOException e) {
                e.printStackTrace();
            }
            StringBuilder string = new StringBuilder();
            byte[] data = new byte[BUFFER_SIZE];
            int bytesRead;
            try {
                while ((bytesRead = gis.read(data)) != -1) {
                    string.append(new String(data, 0, bytesRead));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                gis.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            byte[] byteArray = string.toString().getBytes();
            Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0,byteArray.length);

            if(bitmap != null) {
                return bitmap;
            } else {
                return null;
            }
        }

        return null;
    }

这是我使用上述代码获得的错误消息:

--- SkImageDecoder::Factory returned null

我在PHP中可以很容易地做到这一点,但很难让它在Java中运行!

if(isset($_POST["photo"])) {

    $photoContent = $_POST["photo"];
    $photo = imap_base64 ($photoContent);
    $photo = gzdecode($photo);

    $filename = $_POST["username"].".png";

    $dir = SITE_ROOT_PATH."/images/".$user."/".$filename;
    file_force_contents($dir, $photo);

} else {
    $filename = "NO_PROFILE_PHOTO";
}

2 个答案:

答案 0 :(得分:1)

好的,我设法以这种方式修复了问题:

/**
 * IMPORTANT NOTE!!!!!!!!!!!!!!
 * String is first converted to byte array, then compressed using GZIP and then
 * the resulting byte array is encoded to Base64.DEFAULT
 * @return
 */
public String getProfilePhotoBase64() {
    return mProfilePhotoBase64;
}

public Bitmap getProfilePicture(){
    if(getProfilePhotoBase64() != null) {
        byte[] decoded = Base64.decode(mProfilePhotoBase64.getBytes(), Base64.DEFAULT);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ByteArrayInputStream bis = new ByteArrayInputStream(decoded);

        GZIPInputStream zis = null;
        try {
            zis = new GZIPInputStream(bis);
            byte[] tmpBuffer = new byte[256];
            int n;
            while ((n = zis.read(tmpBuffer)) >= 0) {
                bos.write(tmpBuffer, 0, n);
            }
            zis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Bitmap bitmap = BitmapFactory.decodeByteArray(bos.toByteArray(), 0
                , bos.toByteArray().length);

        if(bitmap != null) {
            return bitmap;
        } else {
            return null;
        }
    }
    return null;
}

答案 1 :(得分:0)

究竟是什么问题? compressAndBase64()将返回一个空字符串,因为boas.toByteArray()将返回0字节,因为boas刚刚创建,因此为空。你不需要创造一个蟒蛇。只需改变

return Base64.encodeToString(bytes, Base64.DEFAULT); 

return Base64.encodeToString(byteArray, Base64.DEFAULT);