我正在使用下面的代码从内部存储卡中读取图像,但我不断收到上述标题异常
public static byte[] convertFileToByteArray(String path) {
File f = new File(path);
byte[] byteArray = null;
try {
InputStream inputStream = new FileInputStream(f);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024 * 8];
int bytesRead = 0;
while ((bytesRead = inputStream.read(b)) != -1) {
bos.write(b, 0, bytesRead);
}
byteArray = bos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG,"e :"+e.getMessage());
}
return byteArray;
}
但图像文件已经在该目录中。我是不是错过了什么。 波纹管代码是保存过程。我正在使用 camera2 api 是否应该将图像转换为位图然后另存为文件??
file = new File(Environment.getExternalStorageDirectory() +
ImageManager.PIC_FILE_NAME );
if (file.exists()) {
file.delete();
}
ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader imageReader) {
Image image = null;
try {
image = reader.acquireLatestImage();
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.capacity()];
buffer.get(bytes);
save(bytes);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
{
if (image != null)
image.close();
}
}
}
private void save(byte[] bytes) throws IOException {
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
outputStream.write(bytes);
} finally {
if (outputStream != null)
outputStream.close();
}
}
};