Android : Save big size image from Camera Application

Shweta

While working with image saving from media/sd-card to android application‘s local path, I came across with an issue to get big size image as an output. Since android is an open source, so vendors can change the inbuilt camera application’s functionality. To handle this scenario I considered three cases as mentioned below.

1) Get big size image from sd-card.
2) Get big size image from media (If sd card is not available).
3) Get small size image if media is not storing image.

The issue was as mentioned below:

My requirement was-

  • Launch Camera Activity to take picture
  • Tell the Camera Activity to save the picture
  • Read the save image at the location you specified and save it with application’s local storage
  • Delete the saved image from the location you specified
  • But the problem is the Camera application has also saved the image in gallery which I don’t want.

Since different devices have different Camera application set as default. So in some devices it might be saved only at one place which is specified by me and some might be saved in Camera folder as well.

This is what the solution which I implemented :

Originally I am using the EXTRA_OUTPUT if sd card is available, if not available then I am checking for media image, if not available then I am using the small image provided by camera application. After saving the image to application’s local path I am removing sd card image as well as gallery image (if image is saved in gallery).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
private String fileName;
private final int CAMERA_REQUEST_CODE = 1234;
private final int SDCARD_REQUEST_CODE = 1235;

// Start Camera app
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri outputFileUri = getOutputUri();
if (outputFileUri != null) {
camera.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(camera, SDCARD_REQUEST_CODE);
} else{
startActivityForResult(camera, CAMERA_REQUEST_CODE);
}

//Return output path as uri where image should be saved
private Uri getOutputUri() {
// If sd card is available
if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)){
String path = Environment.getExternalStorageDirectory() .getAbsolutePath();
fileName = "image_" + System.currentTimeMillis() + ".jpg";
path += "/folder_name";
File file = new File(path);
if (!file.isDirectory()) {
file.mkdirs();
}
path += "/" + fileName;
File imageFile = new File(path);
if (!imageFile.exists()) {
try {
imageFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
return Uri.fromFile(imageFile);
}else{ // If sd card is not available
return null;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Activity result callback
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK && requestCode == SDCARD_REQUEST_CODE){
boolean sdSuccess = saveImageFromSdcard(); // Save image from sd card
if(data!=null && !sdSuccess){
// Save gallery image
boolean mediaSuccess = saveMediaImage(data);
// Image from media not saved
if (!mediaSuccess) {
Bundle b = data.getExtras();
if (b != null) {
Bitmap pic = (Bitmap) b.get("data");
// Save this picture
}
}
// Remove gallery image
Uri dataUri = data.getData();
if (dataUri != null) {
int imageId = Integer.valueOf(dataUri.getLastPathSegment());
removeImage(imageId);
}
}
}else if(resultCode == Activity.RESULT_OK && requestCode == CAMERA_REQUEST_CODE){
if(data!=null){
boolean mediaSuccess = saveMediaImage(data);
if (!mediaSuccess) {
Bundle b = data.getExtras();
if (b != null) {
Bitmap pic = (Bitmap) b.get("data");
// Save this picture
}
}
// Remove gallery image
Uri dataUri = data.getData();
if (dataUri != null) {
int imageId = Integer.valueOf(dataUri.getLastPathSegment());
removeImage(imageId);
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Remove Gallery image
private void removeImage(int id) {
ContentResolver cr = getContentResolver();
int count = cr.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,MediaStore.Images.Media._ID + "=?", new String[] { Long.toString(id) });
}

// Save media image
private boolean saveMediaImage(Intent data){
Uri dataUri = data.getData();
if (dataUri != null) {
// Save Gallery image
File mydir = getDir("application_folder_name", MODE_PRIVATE);
String fileName = "image_" + System.currentTimeMillis() + ".jpg";
// File name
final File f = new File(mydir, fileName);
try {
String mSelectedImagePath = getPath(dataUri);
// Save the image from mSelectedImagePath path
} catch (FileNotFoundException e) {
// Auto-generated catch block
e.printStackTrace();
}
}else{
// image is not in gallery
return false;
}
}
1
2
3
4
5
6
7
8
9
10
11
// Return the data path for specified uri from media content provider
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String path = cursor.getString(column_index);
cursor.close();
cursor = null;
return path;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Save Sd card image
private boolean saveImageFromSdcard() {
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
path += "/folder_name/" + fileName;
File imageFile = new File(path);
if (imageFile.exists()) {
// Save sd card image
File mydir = getDir("application_folder_name", MODE_PRIVATE);
// File name
final File f = new File(mydir, fileName);
try {
// Write image in file from imageFile path
} catch (IOException e1) {
return false;
}
return true;
} else {
// check with gallery image
return false;
}
}
about the author

Shweta

  1. Meera

    November 8, 2012

    Very useful example.Thanks
    here is another useful post that explains saving image in sd card .http://blog.creatiosoft.com/?p=1342

  2. dresses

    November 27, 2012

    I guess, it comes down to 115492 simple choice!

  3. Jyothi

    December 5, 2012

    Thanks Shweta for posting the above example.

  4. Mobile App Development

    December 10, 2012

    It can help alot to developers. Thanks for posting..

  5. Android App development

    January 3, 2013

    Hi there. Nice blog. You have shared useful information. Keep up the good work! This blog is really interesting and gives good details.

  6. Blanche21111

    February 20, 2013

    THANKS! You guys do an awesome website, and have some great contents.

  7. craps betting strategies

    August 14, 2013

    Wow, thanks so much for posting this! It is gonna help when I go to the casino next time! Super Awe Inspiring!

  8. Mobisoft Infotech

    October 22, 2013

    It really helped me a lot. Thanks for sharing.

  9. Pramod Adhikari

    October 22, 2013

    thanks