我正在使用的方法为combine two Bitmap Images
并写入SDCard
。该应用程序。在模拟器中工作正常,但是当我尝试在Real Device上执行时,它通过Null Pointer Exception
在此行cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
即创建一个new Bitmap
,我将在其上绘制要使用Canvas
组合的图像。组合两个位图图像中的空指针异常
现在,在这里combineImages(Bitmap background, Bitmap foreground)
第一个参数是Bitmap from Camera
图片和second is the forefround Gallery item
。从相机拍摄的位图是static Bitmap
,我想这是唯一让我陷入麻烦的东西。所以,有人可以给我一个很好的解决方案,以将Camera
拍摄的照片作为temporary
存储,以便我不会进一步使用它。
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();
}
}
这里是我的logcat输出,当我试图在真实的设备。(LG擎天柱黑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)
我可以知道投票的理由吗? –
你确定线宽和高度cs = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);大于0.似乎无法想象该行中的另一个错误。 – blessenm
是的,因为高度和宽度来自相机拍摄的照片,所以显然它们大于0.谢谢。 –