Android:位图到字节数组并返回:SkImageDecoder::Factory 返回 null
2022-09-03 08:37:24
目标是将 a 转换为 a ,在 a 数据中的活动之间传递它,然后在稍后阶段将其重新转换回 a,以便在 .Bitmap
byte []
Bundle
Bitmap
Imageview
问题是,每当我尝试这样做时,我只得到一个空位图和非描述性的,无用的日志输出:
12-07 17:01:33.282: D/skia(2971): --- SkImageDecoder::Factory returned null
我已经研究了以下解决方案:
突出显示 copyPixelsToBuffer() 在 .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