Camera2,图像作为带有感叹号的空白图像保存到图库

时间:2017-12-31 00:59:24

标签: java android arrays android-camera2

不知道为什么会这样做,这是我的代码,也许我做错了什么并且没有意识到它?

private final ImageReader.OnImageAvailableListener mOnImageAvailableListener = new ImageReader.OnImageAvailableListener() {
    @Override
    public void onImageAvailable(ImageReader reader) {

      mBackgroundHandler.post(new ImageSaver(reader.acquireNextImage()));

    }
};

private class ImageSaver implements Runnable{

    private final Image mImage;

    public ImageSaver(Image image){
        mImage = image;
    }

    @Override
    public void run(){  

       ByteBuffer byteBuffer = mImage.getPlanes()[0].getBuffer();

       byte[] bytes= new byte[byteBuffer.remaining()]; 

       byteBuffer.get(bytes);  

       FileOutputStream fileOutputStream = null;

        try{
            fileOutputStream = new FileOutputStream(mImageFileName); 
            fileOutputStream.write(bytes); 
        }
        catch(IOException e){
            e.printStackTrace();
        }
        finally{  //finally cleans up the resources created in this runnable
            mImage.close();

            if (fileOutputStream !=null){
                try{
                    fileOutputStream.close();
                }
                catch(IOException e){
                    e.printStackTrace();
                }
            }
        }
    }
}

private void createImageFolder() {   
    File imageFile = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    mImageFolder = new File(imageFile, "camera2VideoImage");   
    if (!mImageFolder.exists()) {   
        mImageFolder.mkdirs();
    } 
}


private File createImageFileName() throws IOException {  
    String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
    String prepend = "IMAGE_" + timestamp + "_";
    File imageFile = File.createTempFile(prepend, ".jpg", mImageFolder);
    mImageFileName = imageFile.getAbsolutePath();  
    return imageFile;
}

此代码应该有效,我在Log-cat中没有收到任何错误。我拍摄照片但是当我查看画廊中保存的内容时,图片显示为带有白色环绕感叹号的灰色屏幕。我做错了什么或不包括在内?

0 个答案:

没有答案