调用fileoutputstream.close上的空对象引用?

时间:2016-03-13 13:50:22

标签: android nullpointerexception fileoutputstream bitmapfactory

我试图从Bitmap获取RGB并同时保存JPEG图像,同时在android camera2 api中ByteBuffer提供JPEG字节数组这里是我的代码:

private class ImageSaver implements Runnable {
private final Image mImage;
private ImageSaver(Image image) {
    mImage = image;
}

@Override
public void run() {
    // TODO Auto-generated method stub
    ByteBuffer byteBuffer = mImage.getPlanes()[0].getBuffer();
    byte[] bytes = new byte[byteBuffer.remaining()];
    byteBuffer.get(bytes);
    final byte[] myBytes = bytes;
    Thread getRGB = new Thread() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            super.run();
            Bitmap bmp = BitmapFactory.decodeByteArray(myBytes, 0, myBytes.length);
            for(int y=0; y<bmp.getHeight();y++) {
                for(int x=0; x<bmp.getWidth();x++) {
                    int c = bmp.getPixel(x, y);
                    pixel++;
                    red += Color.red(c);
                    green += Color.green(c);
                    blue += Color.blue(c);
                }
            }
        }           
    };
    getRGB.start();
    FileOutputStream fileOutputStream = null;
    try {
        fileOutputStream = new FileOutputStream(mImageFile);
        fileOutputStream.write(bytes);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        mImage.close();
        if(fileOutputStream!=null)
        {
            try {                       
                fileOutputStream.close();                       
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }       
}   

这里有例外我现在正在

  

03-13 19:26:26.199:E / AndroidRuntime(19715):致命异常:我的Camera2 Basic App   03-13 19:26:26.199:E / AndroidRuntime(19715):进程:com.example.mycamera2basicapp,PID:19715   03-13 19:26:26.199:E / AndroidRuntime(19715):java.lang.NullPointerException:file == null   03-13 19:26:26.199:E / AndroidRuntime(19715):at java.io.FileOutputStream。(FileOutputStream.java:84)   03-13 19:26:26.199:E / AndroidRuntime(19715):at java.io.FileOutputStream。(FileOutputStream.java:72)   03-13 19:26:26.199:E / AndroidRuntime(19715):at com.example.mycamera2basicapp.MainActivity $ ImageSaver.run(MainActivity.java:145)   03-13 19:26:26.199:E / AndroidRuntime(19715):在android.os.Handler.handleCallback(Handler.java:739)   03-13 19:26:26.199:E / AndroidRuntime(19715):在android.os.Handler.dispatchMessage(Handler.java:95)   03-13 19:26:26.199:E / AndroidRuntime(19715):在android.os.Looper.loop(Looper.java:211)   03-13 19:26:26.199:E / AndroidRuntime(19715):在android.os.HandlerThread.run(HandlerThread.java:61)

这里我正在初始化mImageFile:

@Override
            public void onCaptureStarted(CameraCaptureSession session, CaptureRequest request, long timestamp,
                    long frameNumber) {
                // TODO Auto-generated method stub
                super.onCaptureStarted(session, request, timestamp, frameNumber);
                createImageGallery();
                try {
                    mImageFile = createImageFile();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

和createImageFile()是创建JPEG文件的简单函数:

private File createImageFile() throws IOException {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "Jpeg_Image_"+timeStamp+"_";
    File image = File.createTempFile(imageFileName, ".jpg", mJpegGalleryFolder);
    mImageFileLocation = image.getAbsolutePath();
    return image;
}

当我评论这些行

    pixel++;
red += Color.red(c);
green += Color.green(c);
blue += Color.blue(c);
保存

文件,完全没有错误。 我的目的是获取RGB并保存图像,同时有人可以帮助我解决这个问题,或者可以向我展示一些其他方向,我可以保存图像,也可以利用RGB。

2 个答案:

答案 0 :(得分:0)

将条件调用Authorization: Bearer YOUR-ACCESS-TOKEN ,例如

close

假设您使用的是Java 7(或更新版本),您应该使用try-with-resources代替。

答案 1 :(得分:0)

除了Andy的回答,我还想使用这个实用程序类:

public class CloseUtils {

public static void close(AutoCloseable ...autoCloseables) throws Exception {

    Exception firstException = null;

    for (AutoCloseable autoCloseable : autoCloseables) {

        try {
            if (null != autoCloseable) {
                autoCloseable.close();
            } // if
        } catch (Exception e) {

            if (null == firstException) {
                firstException = e;
            } else {
                firstException.addSuppressed(e);
            } // else
        } // catch
    } // for

    if (null != firstException) {
        throw firstException;
    } // if
} // ()

public static Exception closeQuietly(AutoCloseable ...autoCloseables) {
    try {
        close(autoCloseables);
        return null;
    } catch (Exception e) {
        return e;
    } // catch
} // ()


} // class