gpt4 book ai didi

android - 在android的SurfaceView上渲染字节数组

转载 作者:行者123 更新时间:2023-11-29 21:58:25 28 4
gpt4 key购买 nike

您好,我有一个图像的原始转储,我将其存储为字节数组,我必须在表面 View 上显示它。我试图将此字节数组转换为可变位图并在表面支架的 Canvas 上呈现,但在调用“drawBitmap”函数时出现此空指针异常。

BlockquoteE/AndroidRuntime(28358): FATAL EXCEPTION: main E/AndroidRuntime(28358): java.lang.NullPointerException E/AndroidRuntime(28358): at android.graphics.Canvas.throwIfRecycled(Canvas.java:1025) E/AndroidRuntime(28358): at android.graphics.Canvas.drawBitmap(Canvas.java:1065) Blockquote

这就是我尝试呈现字节数组的方式

    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

}

最佳答案

您的字节数组 bytes 永远不会被实例化。因此,当您调用 BitmapFactory.decodeByteArray 时,它会返回 null

来自Android Reference :

public static Bitmap decodeByteArray (byte[] data, int offset, int length, BitmapFactory.Options opts)

Since: API Level 1
Decode an immutable bitmap from the specified byte array.
...

Returns
The decoded bitmap, or null if the image data could not be decoded, or, if opts is non-null, if opts requested only the size be returned (in opts.outWidth and opts.outHeight)

关于android - 在android的SurfaceView上渲染字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12598685/

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