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: Tips4all, CCNA FINAL EXAM
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.
ReplyDeletewidth = 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.
You basically can't get a Null Pointer Exception at the line you indicated, assuming that Bitmap is correctly implemented:
ReplyDeletecs = 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?