组合两个位图图像时出现空指针异常

时间:2011-10-04 06:59:58

标签: android

我正在使用combine two Bitmap Images的方法并写入SDCard。该应用程序。在模拟器中正常工作,但是当我尝试在真实设备上执行时,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();
        }
    }

当我尝试使用Real Device时,这是我的Logcat输出。(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)

2 个答案:

答案 0 :(得分:6)

我猜问题可能是你通过从相机拍摄照片生成的位图的大小。因此,最好尝试使用Bitmap.createScaledBitmap()方法。

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

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

这将根据设备高度宽度的高度来缩放图像。希望这会有所帮助。

答案 1 :(得分:4)

假设Bitmap已正确实现,您基本上无法在指定的行中获得空指针异常:

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

createBitmap是静态的。 Bitmap.Config.ARGB_8888是静态的。 宽度,高度是整数。

什么可能是null?你能发布异常的堆栈跟踪吗?你确定这条线吗?