Skip to main content

Null Pointer Exception in combining two Bitmap Images


I am using a method to combine two Bitmap Images and write in the SDCard . The App. works fine in the emulator, but when I tried to execute in on the Real Device it throughs Null Pointer Exception at this line cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); that is to create a new Bitmap on which I will draw both the images that are to be combined using Canvas .



Now, here in combineImages(Bitmap background, Bitmap foreground) the first argument is the Bitmap from Camera Picture and the second is the forefround Gallery item . The Bitmap taken from Camera is a static Bitmap , I guess that is the only thing that is running me into trouble. So, could someone give me a nice solution to save a picture taken from Camera as a temporary storage so that I doesn't make any issue which using it further.




public void combineImages(Bitmap background, Bitmap foreground) {

Bitmap cs = null;
int width = 0, height = 0;
width = background.getWidth();
height = background.getHeight();
cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas comboImage = new Canvas(cs);
comboImage.drawBitmap(background, 0, 0, null);
comboImage.drawBitmap(foreground, 100, 300, null);

String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png";
OutputStream os = null;
try {
os = new FileOutputStream(Environment.getExternalStorageDirectory() + File.separator + tmpImg);
cs.compress(CompressFormat.PNG, 100, os);
} catch (IOException e) {
e.printStackTrace();
}
}



Here is my Logcat Output when I tried on Real Device.(LG Optimus Black P-970)




10-04 12:36:08.329: ERROR/AndroidRuntime(16356): FATAL EXCEPTION: main
10-04 12:36:08.329: ERROR/AndroidRuntime(16356): java.lang.NullPointerException
10-04 12:36:08.329: ERROR/AndroidRuntime(16356): at android.graphics.Bitmap.createBitmap(Bitmap.java:469)
10-04 12:36:08.329: ERROR/AndroidRuntime(16356): at com.cam.GalleryImageSelected.combineImages(GalleryImageSelected.java:66)
10-04 12:36:08.329: ERROR/AndroidRuntime(16356): at com.cam.GalleryImageSelected$1.onClick(GalleryImageSelected.java:90)
10-04 12:36:08.329: ERROR/AndroidRuntime(16356): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:874)
10-04 12:36:08.329: ERROR/AndroidRuntime(16356): at android.widget.AdapterView.performItemClick(AdapterView.java:294)
10-04 12:36:08.329: ERROR/AndroidRuntime(16356): at android.widget.ListView.performItemClick(ListView.java:3387)
10-04 12:36:08.329: ERROR/AndroidRuntime(16356): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2408)
10-04 12:36:08.329: ERROR/AndroidRuntime(16356): at android.os.Handler.handleCallback(Handler.java:587)
10-04 12:36:08.329: ERROR/AndroidRuntime(16356): at android.os.Handler.dispatchMessage(Handler.java:92)
10-04 12:36:08.329: ERROR/AndroidRuntime(16356): at android.os.Looper.loop(Looper.java:123)
10-04 12:36:08.329: ERROR/AndroidRuntime(16356): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-04 12:36:08.329: ERROR/AndroidRuntime(16356): at java.lang.reflect.Method.invokeNative(Native Method)
10-04 12:36:08.329: ERROR/AndroidRuntime(16356): at java.lang.reflect.Method.invoke(Method.java:521)
10-04 12:36:08.329: ERROR/AndroidRuntime(16356): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:876)
10-04 12:36:08.329: ERROR/AndroidRuntime(16356): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
10-04 12:36:08.329: ERROR/AndroidRuntime(16356): at dalvik.system.NativeStart.main(Native Method)


Source: Tips4allCCNA FINAL EXAM

Comments

  1. I guess the problem might be the size of the Bitmap that you are generating by taking a picture from the Camera. So, better try using Bitmap.createScaledBitmap() method.

    width = getWindowManager().getDefaultDisplay().getWidth();
    height = getWindowManager().getDefaultDisplay().getHeight();

    background = Bitmap.createScaledBitmap(background, width, height, true);


    This will scale your image according to the height-width of the device height-width. Hope this helps.

    ReplyDelete
  2. You basically can't get a Null Pointer Exception at the line you indicated, assuming that Bitmap is correctly implemented:

    cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);


    createBitmap is static.
    Bitmap.Config.ARGB_8888 is static.
    width, height are ints.

    What could possibly be null? Could you post the stack trace of the exception? Are you sure about the line?

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.