上传Bitmap.CompressFormat之前的图像压缩

时间:2016-02-12 15:28:51

标签: android android-studio

我尝试压缩用户从图库中选择的图片进行上传。我看到我的相机图片超过5MB,我想压缩它们(如果可能的话,与facebook相同)。我一直在尝试的事情:

我让用户从图库中选择照片,获取uri并使用它:

File file = new File(getRealPathFromURI(getActivity(), selectedImageUri));
            long length = file.length();
            Log.e("Filesize:", "Before: " + length);
            if (file.getName().toLowerCase().endsWith("jpg")||file.getName().toLowerCase().endsWith("jpeg")){
                Bitmap original;
                try {
                    original = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), selectedImageUri);
                    length = sizeOf(original);
                    Log.e("Filesize:", "BeforeCompression: " + length);
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    original.compress(Bitmap.CompressFormat.JPEG, 50, out);
                    Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
                    length = sizeOf(decoded);
                    Log.e("Filesize:", "AfterCompression: " + length);
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.e("Filesize:", "Error: " + e);
                }

我这样做是为了测试它是否先工作,但我在控制台中得到的是:

/name.company.newapp E/Filesize:: Before: 4970874
/name.company.newapp E/Filesize:: BeforeConversion: 63489024
/name.company.newapp E/Filesize:: AfterConversion: 63489024

尺寸根本没有变化。这是正确的做法吗?

2 个答案:

答案 0 :(得分:1)

This happens because you're actually getting the size of memory used by the Bitmap object by calling sizeOf(bitmap) and not the actual file size.

As you should know, a bitmap operates with the number of pixels in an image. Even though you compress the image using a JPEG compression, the image's width and height do not change. Thus the number of pixels do not change and thus the Bitmap's size (in memory) would not change too.

However, if you save the compressed bitmap to a location in your hard disk and use File.length() to calculate the size of the compressed image, then you will notice the difference.

答案 1 :(得分:-1)

请在解码前和压缩后检查尺寸:

length = sizeOf(original);

我还建议您刷新并关闭输出流:

out.flush();
out.close();

希望我能帮忙!

修改 请尝试以下方法解码您的位图:

       public static Bitmap decodeFile(File f,int WIDTH,int HEIGHT){
    try {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

        final int REQUIRED_WIDTH=WIDTH;
        final int REQUIRED_HEIGHT=HEIGHT;
        //Find the correct scale value. It should be the power of 2.
        int scale=1;
        while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HEIGHT)
            scale*=2;

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

您可以更改图片的宽度和高度,使其缩小。