문제

Hi I have a raw dump of image which i store as byte array i have to display this on surface view. I tried to convert this byte array into a mutable bitmap and render on the canvas of surface holder but i get this null pointer exception when "drawBitmap" function is called.

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

This is how i try to render the byte array

    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          

        }
도움이 되었습니까?

해결책

Your byte array bytes is never instantiated. So, when you call BitmapFactory.decodeByteArray, it returns null.

From the 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)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top