Android BitmapFactory 在 Base64 解码字节数组上返回 null

标签 android base64 bitmapfactory

我有一个服务器,其中有几张照片,大小从 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;
}

和日志

良好的形象:

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

最佳答案

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

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

关于Android BitmapFactory 在 Base64 解码字节数组上返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38087083/

相关文章:

java - 我在哪里定义实体 "context"(未知实体 "Context") - 使用 AIDE

android - 致命信号 11 (SIGSEGV),代码 1,tid 31051 中的故障地址 0x9

java - 在 RecyclerView 中滚动时加载位图无法正常工作

android - 每次我们从主屏幕单击应用程序图标时都会启动应用程序启动屏幕

java - Android:两个 ScaleAnimations 顺序执行

c# - 以 Json : 400 Bad request 发送图像

android - 将图像转换为 Base64 字符串 Android

scripting - 在与请求一起发送之前对变量进行哈希和编码

java - 安卓; BitmapFactory.decodeFile(file) 返回 null,已知文件存在

android - 为什么 BitmapFactory.decodeStream 返回 null?