用字节数组初始化时,位图返回null

时间:2018-11-13 11:07:05

标签: java android arrays android-bitmap

我有位图,我正在使用代码分配字节数组值:

public class LegacyCameraManager implements Camera.ErrorCallback, Camera.PreviewCallback, Camera.AutoFocusCallback, Camera.PictureCallback {
public static Bitmap mBitmap;
    @Override
    public void onPreviewFrame(byte[] bytes, Camera camera) {
        Boolean isProcessing = UserSharedPref.initializeSharedPreferencesForprocessFrames(mContext).getBoolean(UserSharedPref.processFrames, true);
        if (isProcessing) {
            Log.d("TEST:","length of bytes:"+bytes.length);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            mBitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length,options);
            Log.d("TEST:","The bitmap:"+mBitmap);
            tfDetector.onPreviewFrame(bytes, camera);

        }
    }

其余的代码运行良好,并且该方法像应有的那样被连续调用,事情是mBitmap总是注销为“ null”,并且必须将此变量设置为像这样的imageview,如按钮的onclikc这个:

 if (LegacyCameraManager.mBitmap == null) {
            Log.d("TEST:","Bitmaps is NULL!!");
            img.setImageResource(R.drawable.office);
        } else {
            img.setImageBitmap(Bitmap.createScaledBitmap(LegacyCameraManager.mBitmap, img.getWidth(),
                    img.getHeight(), false));
        }

Logcat(很多次应该这样): length of bytes:460800 The bitmap:null

因此,图像未设置为位图为null,实际功能是将图像视图设置为在分配位图时将要发生的瞬间所拍摄的图片,这与预期的一样。我要去哪里错了?

1 个答案:

答案 0 :(得分:-1)

只需尝试一下:

Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0, bitmapdata.length);

返回已解码的位图;如果无法解码图像,则返回null。