18

I want my code to resize the image before saving but I can't find anything about it on Google. Could you help me please ?

This is the code (from Android doc) :

private void galleryAddPic() {
    Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
    File f = new File(mCurrentPhotoPath);

    picturePathForUpload = mCurrentPhotoPath;

    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
}

After that, I have to upload it to a server.

Thanks a lot.

Florian Mac Langlade
  • 1,863
  • 7
  • 28
  • 57
  • Hi Florian Did you have a look at this page: http://stackoverflow.com/questions/12780375/resize-image-after-capture-it-from-native-camera-but-before-save-it-to-sd-card Bonne journée! :) – John Smith Apr 17 '13 at 12:42

6 Answers6

28

You can save Bitmap image following code

Bitmap photo = (Bitmap) "your Bitmap image";
photo = Bitmap.createScaledBitmap(photo, 100, 100, false);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

File f = new File(Environment.getExternalStorageDirectory()
        + File.separator + "Imagename.jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
pablogupi
  • 774
  • 11
  • 27
Prabu
  • 1,441
  • 15
  • 20
6

After reading the other answers and not finding exactly what I wanted, here's my approach at acquiring an appropriately scaled bitmap. This is an adaptation of Prabu's answer.

It makes sure your picture is scaled in a way that does not deform the dimensions of the photo:

public saveScaledPhotoToFile() {
    //Convert your photo to a bitmap
    Bitmap photoBm = (Bitmap) "your Bitmap image";
    //get its orginal dimensions
    int bmOriginalWidth = photoBm.getWidth();
    int bmOriginalHeight = photoBm.getHeight();
    double originalWidthToHeightRatio =  1.0 * bmOriginalWidth / bmOriginalHeight;
    double originalHeightToWidthRatio =  1.0 * bmOriginalHeight / bmOriginalWidth;
    //choose a maximum height
    int maxHeight = 1024;
    //choose a max width
    int maxWidth = 1024;
    //call the method to get the scaled bitmap
    photoBm = getScaledBitmap(photoBm, bmOriginalWidth, bmOriginalHeight,
            originalWidthToHeightRatio, originalHeightToWidthRatio,
            maxHeight, maxWidth);

    /**********THE REST OF THIS IS FROM Prabu's answer*******/
    //create a byte array output stream to hold the photo's bytes
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    //compress the photo's bytes into the byte array output stream
    photoBm.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

    //construct a File object to save the scaled file to
    File f = new File(Environment.getExternalStorageDirectory()
            + File.separator + "Imagename.jpg");
    //create the file
    f.createNewFile();

    //create an FileOutputStream on the created file
    FileOutputStream fo = new FileOutputStream(f);
    //write the photo's bytes to the file
    fo.write(bytes.toByteArray());

    //finish by closing the FileOutputStream
    fo.close();
}

private static Bitmap getScaledBitmap(Bitmap bm, int bmOriginalWidth, int bmOriginalHeight, double originalWidthToHeightRatio, double originalHeightToWidthRatio, int maxHeight, int maxWidth) {
    if(bmOriginalWidth > maxWidth || bmOriginalHeight > maxHeight) {
        Log.v(TAG, format("RESIZING bitmap FROM %sx%s ", bmOriginalWidth, bmOriginalHeight));

        if(bmOriginalWidth > bmOriginalHeight) {
            bm = scaleDeminsFromWidth(bm, maxWidth, bmOriginalHeight, originalHeightToWidthRatio);
        } else {
            bm = scaleDeminsFromHeight(bm, maxHeight, bmOriginalHeight, originalWidthToHeightRatio);
        }

        Log.v(TAG, format("RESIZED bitmap TO %sx%s ", bm.getWidth(), bm.getHeight()));
    }
    return bm;
}

private static Bitmap scaleDeminsFromHeight(Bitmap bm, int maxHeight, int bmOriginalHeight, double originalWidthToHeightRatio) {
    int newHeight = (int) Math.min(maxHeight, bmOriginalHeight * .55);
    int newWidth = (int) (newHeight * originalWidthToHeightRatio);
    bm = Bitmap.createScaledBitmap(bm, newWidth, newHeight, true);
    return bm;
}

private static Bitmap scaleDeminsFromWidth(Bitmap bm, int maxWidth, int bmOriginalWidth, double originalHeightToWidthRatio) {
    //scale the width
    int newWidth = (int) Math.min(maxWidth, bmOriginalWidth * .75);
    int newHeight = (int) (newWidth * originalHeightToWidthRatio);
    bm = Bitmap.createScaledBitmap(bm, newWidth, newHeight, true);
    return bm;
}

Here's a corresponding link to my GitHub Gist: https://gist.github.com/Lwdthe1/2d1cd0a12f30c18db698

lwdthe1
  • 1,001
  • 1
  • 16
  • 16
5

First convert your image to bitmap then use this code:

Bitmap yourBitmap;
Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, newWidth, newHeight, true);
Anchal
  • 859
  • 1
  • 10
  • 21
1

See this it will be help full. In general if your are taking an image by camera using intent you will get the uri of image as result you read it as scale down image and store it in the same place

Arun C
  • 9,035
  • 2
  • 28
  • 42
0

If you want to capture a full size image, then you have no choice but to save it to the sd cart and then change the size of the image.

If however thumbnail image is sufficient then there is no need to save it to SD card and you can extract it from the extras of the returned intent.

You can look at this guide I wrote for both methods of taking images using the build in camera Activity:

Guide: Android: Use Camera Activity for Thumbnail and Full Size Image

Emil Adz
  • 40,709
  • 36
  • 140
  • 187
0
BitmapFactory.Options optionsSignature = new BitmapFactory.Options();
final Bitmap bitmapSignature = BitmapFactory.decodeFile(
fileUriSignature.getPath(), optionsSignature);
Bitmap resizedSignature = Bitmap.createScaledBitmap(
                bitmapSignature, 256, 128, true);
signature.setImageBitmap(resizedSignature);