i am developing an app which captures an image through camera.I firstly save the image in a location, let say an image A whose size 1.9 MB and then i get image from that location, check its size and compress accordingly using bitmapFactory scalefactor lets say it is 525 KB now . Then, what i have to do is upload, while uploading, i save the bitmap image into a file but while saving the bitmap into a file, we need to compress it. Although keeping the quality to 100 but still its quality is again reduced to lets say 100 KB. I dont want this, what i want is to upload the bitmap image at 525KB size not 100 and upload. Please help :(
Ok now let me explain
at first i start the camera intent
Intent callCameraApplicationIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
callCameraApplicationIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException e) {
e.printStackTrace();
}
callCameraApplicationIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photoFile));
here comes createImageFile() method which specifies where to store the captured image
File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHMMSS").format(new Date());
String imageFileName = "IMAGE_" + timeStamp + "_";
File storageDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
boolean tempFile = false;
if (!storageDirectory.exists()) {
tempFile = storageDirectory.mkdir();
}
image = File.createTempFile(imageFileName, ".jpg", storageDirectory);
AppLog.showLog(TAG + " size of image file:::::::::::::::: " + image.length());
imageFileLocation = image.getAbsolutePath();
return image;
}
Now onActivityResult()
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == getActivity().RESULT_OK) {
if (requestCode == REQUEST_CAMERA_FOR_CUSTOMER_IMAGE) {
Bitmap photoCapturedBitmap = decodeSampledBitmapFromResource(imageFileLocation,200, 100);
AppLog.showLog(TAG + " size of photoCapturedBitmap after::::::: " + sizeOf(photoCapturedBitmap));
file=getImageFile(photoCapturedBitmap);
decodeSampledBitmapFromResource() checks the size size and reduces its size accordingly and getImageFile() method keeps that bitmap image into a file where the compression is performed
private File getImageFile(Bitmap imageBitmap) {
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, "demoimage"+".jpg"); // the File to save to
try {
fOut = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut); // saving the Bitmap to a file compressed as a JPEG with 100% compression rate
try {
fOut.flush();
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
// do not forget to close the stream
try {
MediaStore.Images.Media.insertImage(getActivity().getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return
file; }
Now please let whats wrong in this. Any solution is highly appreciated :)