1

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.

Mon Pinoliad
  • 67
  • 1
  • 2
  • 11

2 Answers2

1

Check if the file exists or not before calling BitmapFactory.decodeFile.

Also be sure you have the read/write permissions. Write the following in manifest file.

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

For write purposes you should also give permission at runtime for api >=23.

oiyio
  • 5,219
  • 4
  • 42
  • 54
  • Answer on Gabe Sechan's question. Yes, I have permission both runtime and manifest – Mon Pinoliad Jul 20 '18 at 14:59
  • Ok then check with a certain path and certain file for example put a file manually in pictures folder and test if it works – oiyio Jul 20 '18 at 15:08
0

Ckeck your path, because the decodeFile() expects a file system path. Something like this doesn't work.

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

You need something like

/storage/emulated/0/camera1/pictureblablabla.jpg
Migadepan
  • 1
  • 1
  • As what the error said its just `/storage/emulated/0/IMG_20Jul2018_8716.jpg`. So I assume that the path has no file:// – Mon Pinoliad Jul 20 '18 at 13:51