0

I want to start the camera intent within my app to take a picture and save it to internal storage. I'm using the code of google developers page Capturing images or video. In the processPictureWhenReady method I've implemented the following code to save the picture:

private void processPictureWhenReady(final String picturePath) {
    Log.v("path processPictureWhenReady ", " " + picturePath);
    final File pictureFile = new File(picturePath);

    if (pictureFile.exists()) {
        // The picture is ready; process it.
        try {

            Bitmap imageBitmap = BitmapFactory.decodeFile(picturePath);
            int w = imageBitmap.getWidth();
            int h = imageBitmap.getHeight();

            Bitmap bm2 = Bitmap.createScaledBitmap(imageBitmap, w / 2,
                    h / 2, true);

            imageBitmap = bm2.copy(bm2.getConfig(), true);

            MediaStore.Images.Media.insertImage(getContentResolver(),
                    imageBitmap, "test", "Test");

        } catch (Exception e) {
            Log.e("Exc", e.getMessage());
        }

    }

The camera intent is starting and then I have "tap to accept" to take a picture. But then nothing happens. I have a log message in my onActivityResult method and noticed that the method is not beeing called.

Deno Agüero
  • 519
  • 2
  • 9
  • 27

2 Answers2

0

This is a known issue. I have the same problem. I'm following the case here in the meantime

I've seen people try implementing the preview mode with SurfaceView (I haven't personally gotten it to work but it's worth a shot). Also check here for a similar problem.

Community
  • 1
  • 1
0

I used this method it worked for me very well.

private void processPictureWhenReady(final String picturePath) {
    final File pictureFile = new File(picturePath);
    if(pictureFile.exists()){


    }


    if (pictureFile.exists()) {

    } else {

        final File parentDirectory = pictureFile.getParentFile();
        FileObserver observer = new FileObserver(parentDirectory.getPath()) {
            private boolean isFileWritten;

            @Override
            public void onEvent(int event, String path) {
                if (!isFileWritten) {
                    // For safety, make sure that the file that was created in
                    // the directory is actually the one that we're expecting.
                    File affectedFile = new File(parentDirectory, path);
                    isFileWritten = (event == FileObserver.CLOSE_WRITE && affectedFile.equals(pictureFile));

                   if (isFileWritten) {
                        stopWatching();

                        // Now that the file is ready, recursively call
                        // processPictureWhenReady again (on the UI thread).
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                processPictureWhenReady(picturePath);
                            }
                        });
                    }
                }
            }
        };
        observer.startWatching();
    }
}
Junaid Younad
  • 61
  • 2
  • 8