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).
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;
}
}
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);
}
}
}
}
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;
}
}
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;
}
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;
}
}