0

I currently have a decode bitmap method to optimize the size of the image file, however the input file won’t be a bitmap and will decode to bitmap afterward. So my problem is, how can I resize it if my input is a bitmap already? Thanks.

private Bitmap compressFile(File f) {
    int REQUIRED_SIZE = 80;
    try {
        // decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);

        // Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth;// , height_tmp = o.outHeight;
        int scale = 1;
        while (REQUIRED_SIZE > 0) {
            if (width_tmp <= REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            // height_tmp /= 2;
            scale++;
        }

        // decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {
    }
    return null;
}

What I would like to change to is compressFile (Bitmap f)

4

1 回答 1

2

你也可以用这个

Bitmap b = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
profileImage.setImageBitmap(Bitmap.createScaledBitmap(b, 120, 120, false));

还是您要求:

bitmap.compress(Bitmap.CompressFormat.PNG, quality, outStream);

检查此链接以获取更多信息

于 2013-08-14T11:50:25.257 回答