Rifat
Rifat

Reputation: 1888

Android: BitmapFactory.decodeResource(r,id) - shows NPE to decode image from drawable

I use this method and it works every time if the file is xml and fails if it is in any other format.

Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId);

below will work as it has xml,

Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background);

there is test.PNG image on drawable folder and it wont work:

Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.test);

what is missing in this code to support decoding other format?

I am not specifically scaling the bitmap on test image and it is small too.


enter image description here

Here is the logcat:

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.example.ram.myapplication, PID: 12759
                                                                           java.lang.NullPointerException
                                                                               at android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1083)
                                                                               at android.graphics.Canvas.drawBitmap(Canvas.java:1139)
                                                                               at com.example.ram.myapplication.MySurfaceView.drawSomething(MySurfaceView.java:96)
                                                                               at com.example.ram.myapplication.MySurfaceView$1.surfaceCreated(MySurfaceView.java:69)
                                                                               at android.view.SurfaceView.updateWindow(SurfaceView.java:608)
                                                                               at android.view.SurfaceView.access$000(SurfaceView.java:96)
                                                                               at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:185)
                                                                               at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:847)
                                                                               at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1994)
                                                                               at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1063)
                                                                               at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5993)
                                                                               at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
                                                                               at android.view.Choreographer.doCallbacks(Choreographer.java:574)
                                                                               at android.view.Choreographer.doFrame(Choreographer.java:544)
                                                                               at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
                                                                               at android.os.Handler.handleCallback(Handler.java:733)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:136)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5584)
                                                                               at java.lang.reflect.Method.invokeNative(Native Method)
                                                                               at java.lang.reflect.Method.invoke(Method.java:515)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
                                                                               at dalvik.system.NativeStart.main(Native Method)

Thanks a lot for your time.

Edit : I had to change the directory. Found somewhere - Drawable folder cant be used. So had to use raw folder.

Upvotes: 1

Views: 1581

Answers (2)

Hemant Parmar
Hemant Parmar

Reputation: 3976

Use BitmapFactory.Options

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
Bitmap bitmap= BitmapFactory.decodeResource(getResources(), R.drawable.test, options);

if options.inJustDecodeBounds = false;

If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels.

Hope it will work!!

Upvotes: 2

vikas kumar
vikas kumar

Reputation: 11018

Alternatively, you can try putting your png images into some assets folder and access it something like below.

try {
AssetManager manager = getAssets();
InputStream open = manager.open("ic_launcher_background.png");
Bitmap bitmap = BitmapFactory.decodeStream(open);
} catch (IOException e) {
     e.printStackTrace();
}

Upvotes: 1

Related Questions