How to resize captured image before saving to sdcard?

I am developing an application in which I want to resize the captured image before storing it in the specified sdcard folder.

I used all the permissions needed to write data to the SD card, but still I can not do this.

my code is:

try{
    Bitmap bitmap = Constant.decodeFile(fileName);
    bitmap = Bitmap.createScaledBitmap(bitmap, 480, 320, true);
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);

    File f = new File(fileName.toString());
    FileOutputStream fo = new FileOutputStream(f);
    fo.write(bytes.toByteArray());
        fo.close();

    }catch (Exception e) {
        e.printStackTrace();
    }catch (OutOfMemoryError o) {
        o.printStackTrace();
}
+4
source share
7 answers

You forgot to clear Stream after entering data. use FileOutputStream.flush()after writing data ... and
try this ...

File file = new File(fileName.toString());
try {
    FileOutputStream stream = new FileOutputStream(file);
    bitmap.compress(CompressFormat.PNG, 100, stream);
    stream.flush();
    stream.close();
} catch (Exception e) {
    // TODO: handle exception
}
+1
source

There is a good tutorial on resizing images:

BitmapScaler scaler = new BitmapScaler(getResources(), R.drawable.moorwen, newWidth);
imageView.setImageBitmap(scaler.getScaled());

The BitmapScaler class follows:

class BitmapScaler {
    private static class Size {
        int sample;
        float scale;
    }

    private Bitmap scaled;

    BitmapScaler(Resources resources, int resId, int newWidth)
            throws IOException {
        Size size = getRoughSize(resources, resId, newWidth);
        roughScaleImage(resources, resId, size);
        scaleImage(newWidth);
    }

    BitmapScaler(File file, int newWidth) throws IOException {
        InputStream is = null;
        try {
            is = new FileInputStream(file);
            Size size = getRoughSize(is, newWidth);
            try {
                is = new FileInputStream(file);
                roughScaleImage(is, size);
                scaleImage(newWidth);
            } finally {
                is.close();
            }
        } finally {
            is.close();
        }
    }

    BitmapScaler(AssetManager manager, String assetName, int newWidth)
            throws IOException {
        InputStream is = null;
        try {
            is = manager.open(assetName);
            Size size = getRoughSize(is, newWidth);
            try {
                is = manager.open(assetName);
                roughScaleImage(is, size);
                scaleImage(newWidth);
            } finally {
                is.close();
            }
        } finally {
            is.close();
        }
    }

    Bitmap getScaled() {
        return scaled;
    }

    private void scaleImage(int newWidth) {
        int width = scaled.getWidth();
        int height = scaled.getHeight();

        float scaleWidth = ((float) newWidth) / width;
        float ratio = ((float) scaled.getWidth()) / newWidth;
        int newHeight = (int) (height / ratio);
        float scaleHeight = ((float) newHeight) / height;

        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);

        scaled = Bitmap.createBitmap(scaled, 0, 0, width, height, matrix, true);
    }

    private void roughScaleImage(InputStream is, Size size) {
        Matrix matrix = new Matrix();
        matrix.postScale(size.scale, size.scale);

        BitmapFactory.Options scaledOpts = new BitmapFactory.Options();
        scaledOpts.inSampleSize = size.sample;
        scaled = BitmapFactory.decodeStream(is, null, scaledOpts);
    }

    private void roughScaleImage(Resources resources, int resId, Size size) {
        Matrix matrix = new Matrix();
        matrix.postScale(size.scale, size.scale);

        BitmapFactory.Options scaledOpts = new BitmapFactory.Options();
        scaledOpts.inSampleSize = size.sample;
        scaled = BitmapFactory.decodeResource(resources, resId, scaledOpts);
    }

    private Size getRoughSize(InputStream is, int newWidth) {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is, null, o);

        Size size = getRoughSize(o.outWidth, o.outHeight, newWidth);
        return size;
    }

    private Size getRoughSize(Resources resources, int resId, int newWidth) {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(resources, resId, o);

        Size size = getRoughSize(o.outWidth, o.outHeight, newWidth);
        return size;
    }

    private Size getRoughSize(int outWidth, int outHeight, int newWidth) {
        Size size = new Size();
        size.scale = outWidth / newWidth;
        size.sample = 1;

        int width = outWidth;
        int height = outHeight;

        int newHeight = (int) (outHeight / size.scale);

        while (true) {
            if (width / 2 < newWidth || height / 2 < newHeight) {
                break;
            }
            width /= 2;
            height /= 2;
            size.sample *= 2;
        }
        return size;
    }
}

http://zerocredibility.wordpress.com/2011/01/27/android-bitmap-scaling/

+1
source

createBitmap(), .

:

( , int x, int y, int width, int height)

source ,

x x

y y

height

:

Bitmap newbitmap = Bitmap.createBitmap(originalBitmap, 2, 2, bitmap.getWidth() - 4, bitmap.getHeight() - 120);
+1
+1

PNG-, , PNG - . JPEG 0 100 .

0

? ? , ?

By the way, you should call bitmap.recycle () at the end

0
source
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);
0
source

All Articles