在结合两个位图图像空指针异常位图、指针、图像、异常

2023-09-04 11:22:52 作者:旧颜已不复当年

我使用的方法,以将两个位图图像写在 SD卡。该应用程序。在仿真器工作正常,但是当我试图在其上得来空指针异常这行 CS = Bitmap.createBitmap(真实的设备执行的宽度,高度,Bitmap.Config.ARGB_8888); 这是创建一个新位图关于这一点我将以此既要被图像使用组合画布

现在,在这里 combineImages(位图背景位图前景)第一个参数是从相机的位图图片和二是forefround库项目。从照相机拍摄的位图是一个静态位图,我想这是我的运行陷入困境的唯一的事。因此,可能有人给我一个很好的解决方案,以挽救相机拍摄的照片临时存储,使我没有按'T还用它作任何问题,它。

 公共无效combineImages(位图背景位图前景){

        位图CS = NULL;
        INT宽度= 0,高度= 0;
        宽度= background.getWidth();
        身高= background.getHeight();
        CS = Bitmap.createBitmap(宽度,高度,Bitmap.Config.ARGB_8888);
        帆布comboImage =新的Canvas(CS);
        comboImage.drawBitmap(背景,0,0,NULL);
        comboImage.drawBitmap(前台,100,300,空);

        字符串tmpImg =将String.valueOf(System.currentTimeMillis的())+的.png;
        OutputStream的OS = NULL;
        尝试 {
            OS =新的FileOutputStream(Environment.getExternalStorageDirectory()+文件分割符+ tmpImg);
            cs.com preSS(比较pressFormat.PNG,100,OS);
        }赶上(IOException异常E){
            e.printStackTrace();
        }
    }
 

下面是我的logcat输出,当我试图在真实的设备。(LG擎天柱黑色P-970)

  12月10日至四日:36:08.329:ERROR / AndroidRuntime(16356):致命异常:主要
十二月10号至4日:36:08.329:ERROR / AndroidRuntime(16356):显示java.lang.NullPointerException
十二月10号至4日:36:08.329:ERROR / AndroidRuntime(16356):在android.graphics.Bitmap.createBitmap(Bitmap.java:469)
十二月10号至4日:36:08.329:ERROR / AndroidRuntime(16356):在com.cam.GalleryImageSelected.combineImages(GalleryImageSelected.java:66)
十二月10号至4日:36:08.329:ERROR / AndroidRuntime(16356):在com.cam.GalleryImageSelected $ 1.onClick(GalleryImageSelected.java:90)
十二月10号至4日:36:08.329:ERROR / AndroidRuntime(16356):在com.android.internal.app.AlertController $ AlertParams $ 3.onItemClick(AlertController.java:874)
十二月10号至4日:36:08.329:ERROR / AndroidRuntime(16356):在android.widget.AdapterView.performItemClick(AdapterView.java:294)
十二月10号至4日:36:08.329:ERROR / AndroidRuntime(16356):在android.widget.ListView.performItemClick(ListView.java:3387)
十二月10号至4日:36:08.329:ERROR / AndroidRuntime(16356):在android.widget.AbsListView $ PerformClick.run(AbsListView.java:2408)
十二月10号至4日:36:08.329:ERROR / AndroidRuntime(16356):在android.os.Handler.handleCallback(Handler.java:587)
十二月10号至4日:36:08.329:ERROR / AndroidRuntime(16356):在android.os.Handler.dispatchMessage(Handler.java:92)
十二月10号至4日:36:08.329:ERROR / AndroidRuntime(16356):在android.os.Looper.loop(Looper.java:123)
十二月10号至4日:36:08.329:ERROR / AndroidRuntime(16356):在android.app.ActivityThread.main(ActivityThread.java:4627)
十二月10号至4日:36:08.329:ERROR / AndroidRuntime(16356):在java.lang.reflect.Method.invokeNative(本机方法)
十二月10号至4日:36:08.329:ERROR / AndroidRuntime(16356):在java.lang.reflect.Method.invoke(Method.java:521)
十二月10号至4日:36:08.329:ERROR / AndroidRuntime(16356):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:876)
十二月10号至4日:36:08.329:ERROR / AndroidRuntime(16356):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
十二月10号至4日:36:08.329:ERROR / AndroidRuntime(16356):在dalvik.system.NativeStart.main(本机方法)
 
我的编程经历 28 异常

解决方案

我想这个问题可能是你被拍照的相机生成位​​图的大小。所以,最好尝试使用 Bitmap.createScaledBitmap()方法。

 宽度= getWindowManager()getDefaultDisplay()的getWidth()。
身高= getWindowManager()getDefaultDisplay()的getHeight()。

背景= Bitmap.createScaledBitmap(背景,宽度,高度,真);
 

这将根据高宽设备高度宽度缩放图像。希望这有助于。

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)

解决方案

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.

 
精彩推荐
图片推荐