I am currently developing / experimenting "Analzye Image Application" with Camera 2 API and Microsoft Cognitive - Computer Vision.
Instead of using a normal camera, I used API to capture image and let the bitmap be analyzed by the Computer Vision. What I did here is that I fetch the File Path of the captured image and directly converted it to Bitmap using BitmapFactory. But I always got the error of:
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/IMG_20Jul2018_8112.jpg: open failed: ENOENT (No such file or directory)
I can see the image inside my phone storage but the Bitmap returns null.
Here's my code:
Inside the onCreate
, touchListener (Doubletap to capture the image)
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;
}
});
Inside the takePicture()
function (inserted after //Check orientation 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();
}
Based on the error, it deals something with mBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
What might be the error?
Please base the codes here: Camera 2 API and Microsoft Computer Vision
Thank you in advance guys!
EDIT: Additional Info
I have set user permission to use both camera and access storage.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Also, I requested permission at my runtime. Please refer here.