1

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          

        }
amIT
  • 664
  • 13
  • 27
  • Apart from the fact that you're missing a semicolon in line `byte[] bytes`, `bytes` doesn't seem to be instantiated anywhere. Have you skipped some code in your `surfaceCreated` function? – Nadir Sampaoli Sep 26 '12 at 09:38
  • hi nadir yes i actually i clipped some code where i read byte srray from sockets and store, as i didnt think it was causing any trouble. – amIT Sep 26 '12 at 11:14

1 Answers1

2

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)

Nadir Sampaoli
  • 5,437
  • 4
  • 22
  • 32
  • i checked bmp file is always null added a null check and crash stops but i cant render on surfaceview. – amIT Sep 26 '12 at 11:25
  • @amITsingh is `"/data/dump.txt"` a valid format dump? I think it's not going to work if it's not a valid encoded byte array. Try checking [this](http://stackoverflow.com/questions/6520745/why-does-bitmapfactory-decodebytearray-return-null) and see if it helps – Nadir Sampaoli Sep 26 '12 at 11:42
  • nadir as i mentioned its a raw rgb 888 format dump.. is this a valid format to be decoded into bitmap or only jpeg and png valid formats ? – amIT Oct 11 '12 at 09:50
  • @amITsingh the valid formats are defined by BitmapFactoryOptions.[inPreferredConfig](http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inPreferredConfig). [Here](http://developer.android.com/reference/android/graphics/Bitmap.Config.html#ARGB_8888) are the possible values (you should probably use ARGB_8888, add the alpha layer to your bytearray, then it should work correctly). – Nadir Sampaoli Oct 12 '12 at 07:24
  • yes i added the pixel format to make it work , correct me if i am wrong i should use ARGBX_888 if i want to ignore the alpha channels – amIT Oct 12 '12 at 10:17
  • @amITsingh where did you find about `ARGBX_888`? I haven't found this format anywhere. – Nadir Sampaoli Oct 12 '12 at 13:14
  • its part of android pixel format class http://developer.android.com/reference/android/graphics/PixelFormat.html#RGBX_8888 – amIT Oct 13 '12 at 06:57
  • ah, you meant `RGBX_8888` (not `ARGBX_888`). Anyway, it seems that `inPreferredConfig` are settings for the output. The android documentation are not informative at all about the format of byte array parameter, they just define it as `byte array of compressed image data`. So it seems you need to use a compression format (maybe YUV, who knows). The only advice I can give you is try first to use an `ARGB_8888` array instead of `RGB_888`. If it doesn't work, I'm sorry but I cannot help you any more. – Nadir Sampaoli Oct 13 '12 at 11:12
  • Thanks , ARGB_8888 works i am able to render the raw dump on surfaceview, why i wanted to to know about RGBX_8888 is because my raw dump aplha channels are messy and i dont want to use them. RGBX_8888 works too and to naked eye i cant any difference so i was wondering . – amIT Oct 15 '12 at 05:11