图像覆盖Android Studio

时间:2016-08-04 10:34:15

标签: android image android-studio

我的程序的目标是从SD卡中获取图像,将其传递给CombinePhotos类中的函数(以及另一个图像),该函数将返回Bitmap类型的两个图像之一。我将其覆盖到SD卡中的那个。以下是我的代码,但它一直崩溃。请帮帮我。

private void combinePhotos() throws IOException {
    CombinePhotos combinePhotos = new CombinePhotos();
    Bitmap bm1 = BitmapFactory.decodeFile(Environment.getExternalStorageState() + "/GeoOverlay/pic.jpg");
    Bitmap bm2 = BitmapFactory.decodeResource(this.getResources(), R.drawable.sample);
    Bitmap bmFinal = combinePhotos.combine(bm1, bm2);
    File newFile = new File(Environment.getExternalStorageDirectory()+"/GeoOverlay/pic.jpg");
    FileOutputStream bos = new FileOutputStream(Environment.getExternalStorageDirectory()+"/GeoOverlay/pic.jpg");
    bmFinal.compress(Bitmap.CompressFormat.PNG,85,bos);
    bos.flush();
    bos.close();
}

日志:

08-04 16:12:29.982 2888-5972/com.garnet.dany.geooverlay E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: mounted/GeoOverlay/pic.jpg: open failed: ENOENT (No such file or directory)
08-04 16:12:30.652 2888-5972/com.garnet.dany.geooverlay E/AndroidRuntime: FATAL EXCEPTION: Camera Background
                                                                          Process: com.garnet.dany.geooverlay, PID: 2888
                                                                          java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
                                                                              at com.garnet.dany.geooverlay.CameraClass.combinePhotos(CameraClass.java:262)
                                                                              at com.garnet.dany.geooverlay.CameraClass.access$200(CameraClass.java:53)
                                                                              at com.garnet.dany.geooverlay.CameraClass$5.onImageAvailable(CameraClass.java:201)
                                                                              at android.media.ImageReader$ListenerHandler.handleMessage(ImageReader.java:648)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                              at android.os.Looper.loop(Looper.java:148)
                                                                              at android.os.HandlerThread.run(HandlerThread.java:61)
08-04 16:12:30.702 2888-2888/com.garnet.dany.geooverlay E/AndroidCameraApi: onPause

1 个答案:

答案 0 :(得分:0)

根据共享的日志判断,以下变量看起来为空。

Bitmap bmFinal = combinePhotos.combine(bm1, bm2);

因此,在其上调用方法会导致崩溃。 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference

确保.combine方法确实产生非空输出。