【问题标题】:Android BitmapFactory returning null on Base64 decoded byte arrayAndroid BitmapFactory 在 Base64 解码字节数组上返回 null
【发布时间】:2016-10-31 10:08:51
【问题描述】:

我有一个服务器,其中有几张 1.5 kb 到 9 Mb 的照片。来自 PC、平板电脑和手机的照片。服务器将它们编码为 Base64 字符串,然后将它们发送到 Android 客户端。一张 300 kb 的照片在 BitmapFactory.decodeByteArray 中解码时返回 null...但它是有效的图像并且在在线解码器中解码良好。

byte[] decodedString = Base64.decode(image64, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, ecodedString.length);

2天我找不到答案(

有什么想法吗?谢谢!

附:

 private boolean decodeImage64(String uid, String image64, String name) {
    Bitmap decodedByte;
    boolean result = false;
    if (image64 != null && !image64.isEmpty()) {

        try {
            Log.e(TAG, "decodeImage64: image64.getBytes().length = " + image64.getBytes().length);
            byte[] decodedString = Base64.decode(image64, Base64.DEFAULT);
            Log.e(TAG, "decodeImage64: decodedString = " + decodedString + "  , decodedString.length = " + decodedString.length);
            decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
            Log.e(TAG, "decodeImage64: decodedByte = " + decodedByte);

            if (decodedByte != null) {
                FileOutputStream out = null;
                try {
                    out = new FileOutputStream(getImageFolderName() + "/" + uid + ".png");
                    decodedByte.compress(Bitmap.CompressFormat.PNG, 100, out);
                    decodedByte.recycle();
                    out.close();

                } catch (Exception e) {
                    Log.e(TAG, Log.getStackTraceString(e));
                } finally {
                    try {
                        if (out != null) {
                            out.close();
                        }
                        if (decodedByte != null){
                            decodedByte.recycle();
                        }
                    } catch (IOException e) {
                        Log.e(TAG, Log.getStackTraceString(e));
                    }
                }
                result = true;
            }else {
                Log.e(TAG, "  !!!!!!!!!!!!!!!!!!!!!!! decodeImage64: decodedByte = null "  + name);
            }
        }catch (Exception e){
            Log.e(TAG, Log.getStackTraceString(e));
        }
    } else {
        Log.e(TAG, "decodeImage64: image = null " + name);
    }
    return result;
}

还有logcat

好图:

06-29 02:33:57.465 18197-18584/cps.agrovisio E/myLogs:  ------------------------- doInBackground: Good photo
06-29 02:34:13.993 18197-18584/cps.agrovisio E/myLogs: decodeImage64: image64.getBytes().length = 2264744
06-29 02:34:14.085 18197-18584/cps.agrovisio E/myLogs: decodeImage64: decodedString = [B@bb8956d  , decodedString.length = 1676499
06-29 02:34:14.635 18197-18584/cps.agrovisio E/myLogs: decodeImage64: decodedByte = android.graphics.Bitmap@a6d05a2

图片不好:

06-29 02:33:56.041 18197-18584/сps.agrovisio E/myLogs:  ------------------------- doInBackground: Bad photo 
06-29 02:33:57.177 18197-18584/cps.agrovisio E/myLogs: decodeImage64: image64.getBytes().length = 372570
06-29 02:33:57.194 18197-18584/cps.agrovisio E/myLogs: decodeImage64: decodedString = [B@abcf243  , decodedString.length = 275799
06-29 02:33:57.245 18197-18584/cps.agrovisio E/myLogs: decodeImage64: decodedByte = null

【问题讨论】:

  • 除了大小之外,图像有什么区别?
  • 我的朋友你能把你的图片给我们试试吗?
  • 所有照片 jpg。来自 Android 平板电脑的问题
  • 字符串太大,在txt文件中:dropbox.com/s/3n4doyimeo8v9lz/base64image.txt?dl=0
  • 我认为问题是你的字符串大于 64k,当我想添加到我的项目时它给了我constant string too long 所以你能得到字符串的大小:get string size in bytes

标签: android base64 bitmapfactory


【解决方案1】:

这可能不是您要寻找的答案,但您是否考虑过使用框架?我一直在使用毕加索,它很简单: Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

http://square.github.io/picasso/

【讨论】:

  • 谢谢!但我不能使用毕加索。我在json中有一些参数,base64中的图像之一
【解决方案2】:

image64 分割data:image/jpg;base64, 部分。只有编码的字符串。

你可以使用 substring 方法,它会工作。

【讨论】:

    猜你喜欢
    • 2012-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多