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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex