BitmapFactory:无法解码流.没有这样的文件或目录



我目前正在使用Camera 2 API和Microsoft Cognitive-Computer Vision开发/试验"Anazye Image应用程序"。

我没有使用普通相机,而是使用API来捕捉图像,并让计算机视觉对位图进行分析。我在这里所做的是获取捕获图像的文件路径,并使用BitmapFactory将其直接转换为位图。但我总是犯错误:

E/BitmapFactory:无法解码流:java.io.FileNotFoundException:/storage/emulated/0/IMG_2018_8112.jpg:打开失败:ENOENT(没有这样的文件或目录(

我可以看到手机存储中的图像,但位图返回null。

这是我的代码:

onCreate内部,触摸监听器(双击以捕获图像(

textureView.setOnTouchListener(new View.OnTouchListener() {
private GestureDetector gestureDetector = new GestureDetector(Camera.this, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDoubleTap(MotionEvent e) {
Snackbar.make(findViewById(R.id.textureView), "Capturing...", Snackbar.LENGTH_SHORT).show();
takePicture();
//if(mBitmap == null) {
//    mBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
//}
// START OF COMPUTER VISION
onActivityResult();
// END OF COMPUTER VISION
return super.onDoubleTap(e);
}
// implement here other callback methods like onFling, onScroll as necessary
});
@Override
public boolean onTouch(View v, MotionEvent event) {
gestureDetector.onTouchEvent(event);
return true;
}
});

takePicture()功能内部(插入//根据设备检查方向(:

Date c = Calendar.getInstance().getTime();
System.out.println("Current time => " + c);
SimpleDateFormat df = new SimpleDateFormat("ddMMMyyyy");
// Generate random number
Random r = new Random();
final int currentNumber = r.nextInt((9999 - 1) + 1) + 1;
String fileName = "IMG_" + df.format(c) + "_" + currentNumber + ".jpg";
file = new File(Environment.getExternalStorageDirectory()+"/"+fileName);
//Convert Bitmap to stream
try {
Bitmap bitmap = null;
File f= new File(pathUpload);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
bitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, options);
// Put path into bitmap
mBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
//image.setImageBitmap(bitmap);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
} catch (Exception e) {
e.printStackTrace();
}

基于该错误,对mBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());进行了处理

可能是什么错误?

请在此输入代码:摄像机2 API和Microsoft计算机视觉

提前谢谢各位!

编辑:附加信息

我已经设置了用户使用相机和访问存储的权限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

此外,我在运行时请求了许可。请参阅此处。

在调用BitmapFactory.decodeFile.之前检查文件是否存在

还要确保您具有读/写权限。在清单文件中写入以下内容。

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

出于编写目的,您还应该在运行时授予api>=23的权限。

Ckeck您的路径,因为decodeFile((需要一个文件系统路径。这样的东西不起作用。

file:///storage/emulated/0/camera1/pictureblablabla.jpg

你需要像这样的东西

/storage/emulated/0/camera1/pictureblablabla.jpg

最新更新