0

I want to take a photo from my App and save this in my own gallery. After this I need the full name of the photo. With

private void dispatchTakePictureIntent(int actionCode) {
  Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  this.startActivityForResult(takePictureIntent, actionCode);
}

the photo will be saved in the default gallery. If I read the documentation right than I can set the name with

takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, "MyFileName");

How can I create my own gallery and save the image there instead of the default gallery?

After taking the photo and saving in a gallery the result function will be called. How can I get besides the image the full name of the file.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  switch(requestCode){
    case actionCode:
      // get the image
      InputStream stream = this.getContentResolver().openInputStream(data.getData());

      // How to get the file name?
      break;
    default:
  }
}

After setting the name with the extra the onActivityResult function won't be called. Without this extra I get the image to utilize.

mburm
  • 1,417
  • 2
  • 17
  • 37
  • `"MyFileName"` is also used to set the path of saving. Save them to your own folder, then just load images from there for your gallery? – IAmGroot Nov 13 '13 at 13:03
  • Use this code to save the images in your desired gallery folder [Android saving file to external storage][1] [1]: http://stackoverflow.com/a/7887114/964741 – RajaReddy PolamReddy Nov 13 '13 at 13:07

2 Answers2

1

Capture image like this:

private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private Uri fileUri;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

getOutputMediaFileUri like this:

public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;

/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
  return Uri.fromFile(getOutputMediaFile(type));
}

/** Create a File for saving an image or video */
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;
}

and get saved image fileUri like this:

private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
    if (resultCode == RESULT_OK) {
        // Image captured and saved to fileUri specified in the Intent
        Toast.makeText(this, "Image saved to:\n" +
                 data.getData(), Toast.LENGTH_LONG).show();
    } else if (resultCode == RESULT_CANCELED) {
        // User cancelled the image capture
    } else {
        // Image capture failed, advise user
    }
}

if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
    if (resultCode == RESULT_OK) {
        // Video captured and saved to fileUri specified in the Intent
        Toast.makeText(this, "Video saved to:\n" +
                 data.getData(), Toast.LENGTH_LONG).show();
    } else if (resultCode == RESULT_CANCELED) {
        // User cancelled the video capture
    } else {
        // Video capture failed, advise user
    }
}
}
LL520
  • 159
  • 3
  • I have tested it and it works quite good. My problem is now that the image is saved twice, once in the default gallery with the default name and once at the right place. Have you an idea how I can avoid the save in the default gallery so I would get it only in my save path? – mburm Nov 14 '13 at 11:16
  • The image is saved twice seems depends on the device. See here:http://stackoverflow.com/questions/5221704/camera-intent-activity-avoid-saving-to-gallery?rq=1 ;But you can delete the last photo saved to media storage.See here: http://stackoverflow.com/questions/7499458/how-can-i-stop-mediastore-action-image-capture-duplicating-pictures?rq=1 – LL520 Nov 14 '13 at 13:02
0

you might want to check this open source..IT has what you need.

https://github.com/Orange1987/Android

Sandeep R
  • 2,284
  • 3
  • 25
  • 51