【发布时间】:2018-07-20 13:23:23
【问题描述】:
我目前正在使用 Camera 2 API 和 Microsoft Cognitive - Computer Vision 开发/试验“Analzye 图像应用程序”。
我没有使用普通相机,而是使用 API 来捕获图像并让计算机视觉分析位图。我在这里所做的是获取捕获图像的文件路径并使用 BitmapFactory 直接将其转换为位图。但我总是得到以下错误:
E/BitmapFactory:无法解码流:java.io.FileNotFoundException:/storage/emulated/0/IMG_20Jul2018_8112.jpg:打开失败:ENOENT(没有这样的文件或目录)
我可以在手机存储中看到图像,但位图返回 null。
这是我的代码:
在onCreate里面,touchListener(双击抓图)
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() 函数内部(插入在 //Checkorientation base on device 之后):
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());
可能是什么错误?
请在此处建立代码: Camera 2 API 和 Microsoft Computer Vision
提前谢谢你们!
编辑:附加信息
我已设置用户权限以使用相机和访问存储。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
另外,我在运行时请求了许可。请参考here。
【问题讨论】:
-
您是否有权读取该文件?您是否在运行时请求读取权限(不仅仅是在您的清单中)?
-
@GabeSechan 是的,先生。我提到了这个链接。 link
-
请使用 File.seperator 而不是“/”。你能检查一下 file.exists() 返回什么吗?在调用解码文件之前,你应该添加 do if(file.exists())
-
@oiyio 相同的错误输出。我试过
file.exists和它的空值。 -
null 什么? file.exists() 返回 false 或 true。
标签: android microsoft-cognitive