0

I use the code from the developer-website https://developer.android.com/training/camera/photobasics.html

btnPhoto.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Log.d("Button", "take photo");
                dispatchTakePictureIntent();
            }
        });
    }
    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(photoFile));
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }
    }
    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        Log.d("storageDir",storageDir.toString());
        //File storageDir = "/Android/data/" + getApplicationContext().getPackageName() + "/files/";
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = "file:" + image.getAbsolutePath();
        return image;
    }

If the button is pushed the intent is started, I can take a photo, the camera is closed but no photo is taken (no photo in any folder). I don't know why, the code should work.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Piet
  • 395
  • 2
  • 7
  • 17

1 Answers1

0

How did you come to the conclusion that there is no file in the Pictures folder? When you create any new file in Android, it doesn't get reflected immediately. To make that reflect in gallery, you need to scan it via MediaScannerConnection.

MediaScannerConnection.scanFile(getActivity(), new String[]{image.toString()}, null, null);

But if that's not the case: In your onActivityResult(), query the intent object that you received and see if you have the Bitmap of the image you captured.

I hope this helps : capturing images with MediaStore.ACTION_IMAGE_CAPTURE intent in android

Community
  • 1
  • 1
Henry
  • 17,490
  • 7
  • 63
  • 98
  • coz when I browse to the folder with any filemanager and there is no file, I think there is no file. It has nothing to do with the media-scanner – Piet Oct 15 '15 at 19:39
  • @Piet The filemanager, or if you are connected to a PC, wont show the newly created file until it's scanned. Also, try to obtain the file back from the code itself and see if the file exists. – Henry Oct 16 '15 at 01:24