4

how can I save a Bitmap Image to the sdcard with loosing it's original size or quality, I am currently using this code, but it always saves the image much smaller than the original size of the image captured by the camera.


private void SaveImage(Bitmap bitmap, String strType) {
        File sdImageMainDirectory = new File("/sdcard/");
        if (sdImageMainDirectory.exists()) {
            FileOutputStream fileOutputStream = null;
            Date dt = new Date();
            String nameFile = "myImage";
            int quality = 100;

        try {
            String strFullImageName;
            strFullImageName = UserID + "_" + nameFile + ".jpg";
            fileOutputStream = new FileOutputStream(
                    sdImageMainDirectory.toString() + "/"
                            + strFullImageName);
            BufferedOutputStream bos = new BufferedOutputStream(
                    fileOutputStream);
            bitmap.compress(CompressFormat.JPEG, quality, bos);
            bos.flush();
            bos.close();
            UploadImage(bitmap, strFullImageName, strType);
        } catch (FileNotFoundException e) {
            Utilities.LogError(e);
        } catch (IOException e) {
            Utilities.LogError(e);
        } catch (Exception eee) {
            Utilities.LogError(eee);
        }

    } else {
        Utilities.LogError("File not Found");
    }
}

Found the solution,

After following the link provided by KPBird, I found that the problem was in how the camera activity was returning the bitmap object captured.

To preserver memory, the camera activity returns a scaled image of the captured photo, so the solution was to never use this scaled image!

Just pass an extra to the camera intent, providing the url where you want to save the image and voilaa!, the captured photo is saved where you want it, with the original scale

Code:



private static final int CAMERA_PIC_REQUEST = 1337;

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

imageNameToUpload =  "myImage.jpeg";

cameraIntent.putExtra( android.provider.MediaStore.EXTRA_OUTPUT,        Uri.fromFile(new File(                  imagePathToUpload+imageNameToUpload)));
                                startActivityForResult(cameraIntent,
                                        CAMERA_PIC_REQUEST);

Thanks KPBird!

Hassan Mokdad
  • 5,832
  • 18
  • 55
  • 90

1 Answers1

3

Visit following link

How do I save data from Camera to disk using MediaStore on Android?

It may help

Community
  • 1
  • 1
Ketan Parmar
  • 2,962
  • 19
  • 27
  • I tried, the link, it works on adding an extra , and the camera is now saving the captured image as it should and in the full size, but after I hit the save button on the camera, the whole device freeze, weird, is this a doumented bug? – Hassan Mokdad May 19 '11 at 07:21
  • the extra was added like this:cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File("/sdcard/tmphassamok.jpeg"))); – Hassan Mokdad May 19 '11 at 07:22