Decompressing Bitmap from Base64 and GZIP
In my application I get a profile picture for user then I save it to a serialized class as string.
 I do the GZIP compress and Base64 using the code below, but I can not do the reverse thing as you see in the getProfilePicture() method further down:  
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);
}
Now I want to convert it back to Bitmap...but so far I didn't succeed to.
The steps are decoding back the string from Base64 to byte array then decompress the byte array and convert to 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;
    }
This is the error message I get using the code above:
--- SkImageDecoder::Factory returned null
I can do this quite easy in PHP, but its darn hard to make it work in 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";
}
好吧,我设法解决这个问题:
/**
 * 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;
}
And what is the problem exactly? compressAndBase64() will return an empty string as boas.toByteArray() will return 0 bytes as boas is just created and hence empty. You don't need to create a boas. Just change
return Base64.encodeToString(bytes, Base64.DEFAULT); 
to
return Base64.encodeToString(byteArray, Base64.DEFAULT);
                        链接地址: http://www.djcxy.com/p/92422.html
                        上一篇: 如何解压缩gzip数据?
下一篇: 从Base64和GZIP解压缩位图
