Pick image from sd card, resize the image and save it back to sd card

Viewed 31675

I am working on an application, in which I need to pick an image from sd card and show it in image view. Now I want the user to decrease/increase its width by clicking a button and then save it back to the sd card.

I have done the image picking and showing it on ui. But unable to find how to resize it.Can anyone please suggest me how to achieve it.

7 Answers

Here my static method:

public static void resizeImageFile(File originalImageFile, File resizedImageFile, int maxSize) {
    Bitmap bitmap = BitmapFactory.decodeFile(originalImageFile.getAbsolutePath());
    Bitmap resizedBitmap;
    if (bitmap.getWidth() > bitmap.getHeight()) {
        resizedBitmap = Bitmap.createScaledBitmap(bitmap, maxSize, maxSize * bitmap.getHeight() / bitmap.getWidth(), false);
    } else {
        resizedBitmap = Bitmap.createScaledBitmap(bitmap, maxSize * bitmap.getWidth() / bitmap.getHeight(), maxSize, false);
    }

    FileOutputStream fileOutputStream;
    try {
        fileOutputStream = new FileOutputStream(resizedImageFile);
        resizedBitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
        fileOutputStream.flush();
        fileOutputStream.close();
        bitmap.recycle();
        resizedBitmap.recycle();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Related