gpt4 book ai didi

java - Android:位图到字节数组并返回:SkImageDecoder::Factory 返回 null

转载 作者:太空狗 更新时间:2023-10-29 22:59:16 25 4
gpt4 key购买 nike

目标是将 Bitmap 转换为 byte [],在 Bundle 数据的 Activity 之间传递它,然后重新转换它在稍后阶段返回到 Bitmap 以在 Imageview 中显示。

问题是每当我尝试这个时,我只会得到一个空位图和非描述性、无用的日志输出:

12-07 17:01:33.282: D/skia(2971): --- SkImageDecoder::Factory 返回 null

我看过以下解决方案:

Solution supplies the bitmap to byte[] code used

Highlighted that copyPixelsToBuffer() is essential over .compress

(特别是在这种情况下没有必要)。

我已经运行了以下测试用例,它明确地将问题缩小到转换和恢复代码。根据我的调试,解码正确,字节数组大小正确且完整,位图配置强制相同,decodeByteArray 只是失败:

package com.example.debug;

import java.nio.ByteBuffer;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {
RelativeLayout rl = null;
RelativeLayout.LayoutParams rlp = null;

ImageView ivBef = null;
ImageView ivAft = null;

Bitmap bmBef = null;
Bitmap bmAft = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TEST
BitmapFactory.Options bmo = new BitmapFactory.Options();
bmo.inPreferredConfig = Config.ARGB_8888;

bmBef = BitmapFactory.decodeFile("/mnt/sdcard/Debug/001.png", bmo);
byte[] b = bitmapToByteArray(bmBef);
bmAft = BitmapFactory.decodeByteArray(b, 0, b.length, bmo);

LinearLayout ll = new LinearLayout(this);

ivBef = new ImageView(this);
ivBef.setImageBitmap(bmBef);

ivAft = new ImageView(this);
ivAft.setImageBitmap(bmAft);

ll.addView(ivBef);
ll.addView(ivAft);

setContentView(ll);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

public static byte[] bitmapToByteArray(Bitmap bm) {
// Create the buffer with the correct size
int iBytes = bm.getWidth() * bm.getHeight() * 4;
ByteBuffer buffer = ByteBuffer.allocate(iBytes);

// Log.e("DBG", buffer.remaining()+""); -- Returns a correct number based on dimensions
// Copy to buffer and then into byte array
bm.copyPixelsToBuffer(buffer);
// Log.e("DBG", buffer.remaining()+""); -- Returns 0
return buffer.array();
}

}

之前的 Imageview 正确显示图像,之后的 ImageView 什么都不显示(正如您对空位图所期望的那样

最佳答案

您正在将 Bitmap 传递到 Intent 并在下一个 Activity 中从 bundle 获取位图,但问题是如果您的 Bitmap/Image 尺寸当时很大,则图像不会在下一个 Activity 中加载。

使用以下 2 种解决方案来解决此问题。

1) 首先将图像转换为字节数组,然后传递给 Intent,在下一个 Activity 中,从 Bundle 中获取字节数组并转换为图像(位图)并设置到 ImageView 中。

将位图转换为字节数组:-

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

将字节数组传递给 Intent :-

Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);

从 Bundle 中获取字节数组并转换为位图图像:-

Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");

Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);

image.setImageBitmap(bmp);

2) 首先将图像保存到 SDCard 中,然后在下一个 Activity 中将此图像设置到 ImageView 中。

关于java - Android:位图到字节数组并返回:SkImageDecoder::Factory 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13758560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com