7

I have been working on a camera app, I want it to be when I press the "Capture" button it takes a picture and saves it to the SDcard so it can be viewed in the gallery.

However at current it fails to save how I want it to. At current when I press capture it takes a picture, but the picture is only shown in the gallery after I fully restart my phone.

This problem has been messing with me for weeks, I have mostly followed the tutorial android gives.

Here is my code for the main class which handle pictures.

 private PictureCallback mPicture = new PictureCallback() 
   {

    @Override
    public void onPictureTaken(byte[] data, Camera camera)
      {
        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
        if (pictureFile == null){
           Log.d(TAG, "Error creating media file, check storage permissions: ");
            return;
        }

        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
        } catch (FileNotFoundException e) {
          //  Log.d(TAG, "File not found: " + e.getMessage());
        } catch (IOException e) {
        //    Log.d(TAG, "Error accessing file: " + e.getMessage());
        }
    }
};


private static File getOutputMediaFile(int type){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "MyCameraApp");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "IMG_"+ timeStamp + ".jpg");
    } else if(type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "VID_"+ timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}

public void onClick(View v) {

    mCamera.takePicture(null, null, mPicture);
    Context context = getApplicationContext();
    CharSequence text = "Click Detected";
    int duration = Toast.LENGTH_SHORT;

    Toast toast = Toast.makeText(context, text, duration);
    toast.show();

  }

My log cat shows the following when I click capture

Level: E Tag: Camera Text: in handlemessage for CAMERA_MSG_RAW_IMAGE

My permissions are

uses-permission android:name="android.permission.CAMERA"
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" 

Any help is much appreciated. Thanks

Miral Dhokiya
  • 1,720
  • 13
  • 26
Tom
  • 73
  • 1
  • 4

3 Answers3

6

This question is a duplicate of Image, saved to sdcard, doesn't appear in Android's Gallery app (especially the answer by ShadowGod)

To fix your code, add the following to onPictureTaken after fos.close()

File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
    Environment.DIRECTORY_PICTURES), "MyCameraApp");
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
    Uri.parse("file://"+ mediaStorageDir)));
Community
  • 1
  • 1
Adrian G
  • 775
  • 6
  • 11
3
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

Does not seem to work on KITKAT. It throws permission denial exception and crashes the app. So for this, I have done the following,

String path = mediaStorageDir.getPath() + File.separator
                + "IMG_Some_name.jpg";
CameraActivity.this.sendBroadcast(new Intent(
                         Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri
                        .parse("file://" + path)));

Hope it helps.

Muhammad Riyaz
  • 2,832
  • 2
  • 26
  • 30
0

MediaScannerConnection works for updating the gallery with videos or images in KitKat, examples and code listed in:

Image, saved to sdcard, doesn't appear in Android's Gallery app

Android MediaStore insertVideo

http://developer.android.com/reference/android/media/MediaScannerConnection.html

Community
  • 1
  • 1
Greg Marsh
  • 209
  • 1
  • 2
  • 9