Android 字节数组到位图的转换问题
我正在努力实施。我需要将字节[]转换为位图而无需压缩。但每次我都会看到整个黑色的画面。怎么做 我在做什么:Android 字节数组到位图的转换问题,android,arrays,android-bitmap,Android,Arrays,Android Bitmap,我正在努力实施。我需要将字节[]转换为位图而无需压缩。但每次我都会看到整个黑色的画面。怎么做 我在做什么: int bytes = bmp.getByteCount(); ByteBuffer buffer = ByteBuffer.allocate(bytes); bmp.copyPixelsToBuffer(buffer); byte[] resarray = buffer.array(); 下
int bytes = bmp.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes);
bmp.copyPixelsToBuffer(buffer);
byte[] resarray = buffer.array();
下面是我如何将其转换为位图:
BitmapFactory.Options options = new
BitmapFactory.Options();
options.inScaled = false;
Bitmap bmp = BitmapFactory.decodeByteArray(barray,0, barray.length,options);
ImageView imageView = (ImageView) findViewById(R.id.resdetayimage);
imageView.setImageBitmap(bmp);`
编辑
位图工厂只解码压缩的东西。这就是我的代码不起作用的原因。所以我需要像这样的东西:
Bitmap.Config configBmp = Bitmap.Config.valueOf(bitmap.getConfig().name());
Bitmap bitmap_tmp = Bitmap.createBitmap(width, height, configBmp);
ByteBuffer buffer = ByteBuffer.wrap(byteArray);
bitmap_tmp.copyPixelsFromBuffer(buffer);
但代码仍然不起作用。我如何实现这一点?我从intent获得字节[]。将位图转换为字节数组
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, bStream);
byte[] mByteArray = bStream.toByteArray();
将byteArray转换为位图
Bitmap bitmap = BitmapFactory.decodeByteArray(mByteArray , 0, mByteArray.length);
使用
copyPixelsFromBuffer()
因为它与copyPixelsToBuffer()
相反,我确信,但您可以尝试创建位图,即位图X=bitmap.createBitmap(resarray,bmp.getWidth(),bmp.getHeight(),bmpgetConfig());您应该执行与copyPixelsToBuffer()相反的操作。那么是否有从缓冲区读取像素之类的东西?DecodeByteArray需要该数组中的jpg文件。不是像素。我认为它现在返回空值。请查收。所以不是一个黑色的图像。它不返回空值已经被选中了。我不会压缩它