【问题标题】:BitmapFacotry.decodeByteArray returns null for a valid image's byte arrayBitmapFactry.decodeByteArray 为有效图像的字节数组返回 null
【发布时间】:2013-09-05 21:54:57
【问题描述】:

这是 mp3 文件标签中的图像 http://i.stack.imgur.com/HtXqA.jpg

我使用 Android 的 MediaMetaDataRetreiver 从 mp3 文件中获取此图像字节数组...当我尝试使用 BitmapFactory.decodeByteArray 解码此图像时,它返回 null

不胜感激

编辑:当我第一次使用 options.inJustDecodeBounds = true ... options.outWidth 和 options.outHeight 解码时返回正确的宽度和高度

完整代码:

MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        retriever.setDataSource(context, songUri);
        byte[] data = retriever.getEmbeddedPicture();
        if(data != null)
        {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeByteArray(data, 0, data.length, options);
            options.inSampleSize = BitmapUtils.calculateInSampleSize(options, 500, 500);
            options.inJustDecodeBounds = false;
            albumArtBitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);
        }

【问题讨论】:

  • 你能贴出你的代码吗?
  • 确实确实验证了 byte[] 实际上是您链接的文件? (例如,通过将其转储到文件系统,或在其上测量校验和)
  • 您可以记录您为 inSampleSize 检索到的值吗?\
  • @njzk2 ...我将字节数组作为 jpg 转储到 sd ... android 无法显示图像(QuickPic -> 加载失败)...但是当我将其发送到我的计算机时图像显示正确......所以wtf编辑:是的文件与我发布的文件相同
  • @njzk2 ... inSampleSize 的值为 4,在 logcat 中有一个带有 tag="skia" 的日志,其 text="---decoder->decode 返回 false"

标签: android bitmap


【解决方案1】:

改用流:

InputStream is;
    Bitmap bitmap;
    is = context.getResources().openRawResource(R.drawable.someImage);

    bitmap = BitmapFactory.decodeStream(is);
    try {
        is.close();
        is = null;
    } catch (IOException e) {
    }

顺便说一句,我还没有尝试过 jpg,但我想这可能是个问题。尝试将其转换为“png”

【讨论】:

  • 这张图片是从一个 mp3 文件的标签中提取的......无法为其获取流......只有字节数组......顺便说一句,我也尝试使用 ByteArrayInputStream 作为字节数组并进行解码它使用 BitmapFactory.decodeStream 并且它不起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多