-2

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 :)

AQhtar Ali
  • 51
  • 2
  • 11
  • 2
    Your question is not clear – Ujju Feb 25 '16 at 11:30
  • @Ujju - let me know which part of the question is not clear :) All i want to do is store an already reduced size image into a file without re-reducing its size. – AQhtar Ali Feb 25 '16 at 12:12
  • Image size does not reduce if the quality is 100,infact it increases the size in case of jpeg, maybe somthing else is reducing the size, so like yu said with 100 quality u won't get 100kb image, – Ujju Feb 25 '16 at 17:15
  • Check this url. it will helpful for you. bitmap.compress(Bitmap.CompressFormat.PNG, quality, outStream); http://stackoverflow.com/questions/7769806/convert-bitmap-to-file – Karthik Sridharan Feb 25 '16 at 11:28
  • i have posted my codes. Will u now please let me know what wrong i m doing in my codes. – AQhtar Ali Feb 26 '16 at 07:17

1 Answers1

0

Use this Code this will be help you to store bitmap into file

private boolean storeImage(Bitmap imageData, String filename) {
//get path to external storage (SD card)
String iconsStoragePath = Environment.getExternalStorageDirectory() + "/myAppDir/myImages/"
File sdIconStorageDir = new File(iconsStoragePath);

//create storage directories, if they don't exist
sdIconStorageDir.mkdirs();

try {
    String filePath = sdIconStorageDir.toString() + filename;
    FileOutputStream fileOutputStream = new FileOutputStream(filePath);

    BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);

    //choose another format if PNG doesn't suit you
    imageData.compress(CompressFormat.PNG, 100, bos);

    bos.flush();
    bos.close();

     //here report
} catch (FileNotFoundException e) {
    Log.w("TAG", "Error saving image file: " + e.getMessage());
    return false;
} catch (IOException e) {
    Log.w("TAG", "Error saving image file: " + e.getMessage());
    return false;
}

return true;

 }
Khaledonia
  • 2,054
  • 3
  • 15
  • 33
New Coder
  • 873
  • 8
  • 14
  • while compressing with that line of code imageData.compress(CompressFormat.PNG, 100, bos); the quality of image again reduced which i dont want. Let me know if u have any references to that. Thank You ! – AQhtar Ali Feb 26 '16 at 07:04