Images not being saved when picture is taken by ca

2019-04-11 16:48发布

问题:

I'm currently trying to save images taken from a phone to its gallery, but the code below only works if I choose the stock camera app when the chooser dialog pops up. Whenever I choose another camera app(e.g., the Google camera), the taken picture doesn't get saved any where.

To make things even stranger, sometimes the picture does show up in its designated directory in the gallery, but after 15 mins or so, the same goes for when I use the stock camera app: the picture will get saved in the default camera shots directory, but takes quite a bit to show up in its designated directory, if it shows up 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);

}

回答1:

well ,
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); does not work anymore . you should do something like this : call Camera Activity :

  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:

It turns out my code was working after all. The pictures were being saved in the new directory, but the problem was that the gallery wasn't being updated, which explains why the photos would randomly appear in the directory later on. Being new to this, it never occurred to me that I would have to update the gallery. I only came to this realization after using ES File Explorer to look through my files. To fix my problem, I just made a new method in my CameraFragment that would call on the media scanner. I called this method from onActivityResult().

Here's the new method, though there's nothing really "new" about it since I ran into the same code on 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 could handle the camera intent if I'm not giving the option to use choose a picture from a gallery, so I'm going to remove all that from my question.