嗨,我有一个原始图像转储,我存储为字节数组,我必须在表面视图上显示它。我试图将此字节数组转换为可变位图并在表面支架的画布上渲染,但在调用“drawBitmap”函数时出现此空指针异常。
BlockquoteE/AndroidRuntime(28358): 致命异常: main E/AndroidRuntime(28358): java.lang.NullPointerException E/AndroidRuntime(28358): at android.graphics.Canvas.throwIfRecycled(Canvas.java:1025) E/AndroidRuntime(28358) ): 在 android.graphics.Canvas.drawBitmap(Canvas.java:1065) 块引用
这就是我尝试渲染字节数组的方式
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
//getting byte array
byte[] bytes;
File f1= new File("/data/dump.txt");
int offset = 0, n = 0;
InputStream in;
long length = f1.length();
bytes = new byte[(int) length];
try {
in = new FileInputStream(f1);
while (offset < bytes.length
&& (n = in.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += n;
}
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// getting byte array
Bitmap bmp;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length,options );
Canvas canvas = holder.lockCanvas();
if(canvas != null){
canvas.drawBitmap(bmp, 0, 0, null);
}
holder.unlockCanvasAndPost(canvas); //finalize
}