Images are not saved when shooting with a camera application other than a camera

I am currently trying to save images taken from my phone to my gallery, but the code below only works if I select the camera app when the select dialog appears. Whenever I select another camera app (like google camera) the captured image is not saved anywhere.

To make things even weirder, sometimes the image appears in its designated directory in the gallery, but after 15 minutes or so, the same thing happens when I use my camera app: the image will be saved by the default camera, but it takes quite a bit to appear in its designated directory, if it appears there at all.

// Capturing Camera Image will launch camera app request image capture
void captureImage() {
    //file uri to store image.
    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

    // Request camera app to capture image
    Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

    // start the image capture Intent
    getActivity().startActivityForResult(captureIntent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);

}

      

+3


source to share


2 answers


It turns out my code worked in the end. The pictures were saved in the new directory, but the problem was that the gallery was not updating, which explains why the pictures would accidentally appear in the directory later. As a newbie to this, it never occurred to me that I would have to update the gallery. I just came to this implementation after using ES File Explorer to view my files. To fix my problem, I just made a new method in my CameraFragment that will call the media scanner. I called this method from onActivityResult ().

Here's a new method, although there is nothing "new" in it, since I ran into the same code in other SO questions:



protected void mediaScan() {
    getActivity().sendBroadcast(
            new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, 
                    Uri.parse(fileUri.toString())));
}

      

I also don't need to call the package manager and iterate through the apps that can handle the camera intent if I don't enable the gallery image selection, so I'm going to remove everything from my question.

0


source


ok,
   intent.putExtra (MediaStore.EXTRA_OUTPUT, fileUri); does not work any more. you should do something like this: call the camera action:

  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

      

and onActivityResult:



 if (data.getData() == null) {  
    Bitmap bm = (Bitmap)
  data.getExtras().get("data");
   String timeStamp = new SimpleDateFormat(
                            "yyyyMMdd_HHmmss").format(new Date());

  File pictureFile = new File(Environment
                            .getExternalStoragePublicDirectory(
                                    Environment.DIRECTORY_PICTURES)
                            .getAbsolutePath()
                            + File.separator + "IMG_" + timeStamp);

try {
     FileOutputStream fos = new FileOutputStream(
                                pictureFile);
    bm.compress(Bitmap.CompressFormat.JPEG, 90, fos);
    fos.close();
     String filePath = pictureFile.getAbsolutePath();
   } catch (FileNotFoundException e) {
    e.printStackTrace();
   } catch (IOException e) {
   e.printStackTrace();
   }   } else {

   Uri imgUri =data.getData());  

      

}

+2


source







All Articles