Android位图不会压缩

问题描述:

我试图压缩从用户的图库或相机拍摄的位图,并将其作为配置文件图片存储在分析服务器中。Android位图不会压缩

问题是位图不会压缩。该图像保存完美,并可在数据库中使用,但文件大小仅适用于个人资料图片。

这里是我当前的代码:

//Compressing 
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
profilePictureBitmap.compress(Bitmap.CompressFormat.PNG, 20, stream); 
byte[] image = stream.toByteArray(); 

//Saving 
String imageName = username + "_profile_picture.png"; 
final ParseFile file = new ParseFile(imageName, image); 
file.saveInBackground(new SaveCallback() { 
    @Override 
    public void done(ParseException e) { 
     if(e == null) { 
      user.put("profilePicture", file); 
      user.signUpInBackground(); 
     } 
    } 
} 

我使用的是图片选择器库,获取图像的路径。然后我把它变成一个位图。

继承人我的代码来获取图像:

ArrayList<Image> images = data.getParcelableArrayListExtra(ImagePickerActivity.INTENT_EXTRA_SELECTED_IMAGES); 
if(images.size() > 0) { 
    Image image = images.get(0); 
    File imgFile = new File(image.getPath()); 
    if(imgFile.exists()){ 
     profilePictureBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); 
     profilePictureImage.setImageBitmap(profilePictureBitmap); 
    } 
} 

如果有关于如何解决这一问题我将不胜感激的任何想法。 感谢:]

+1

PNG不使用有损压缩算法。将JPEG用于照片,例如个人资料照片。 – CommonsWare

+0

'从用户的画廊或相机中获取的位图。相机应用程序和图库应用程序不提供位图开始。 – greenapps

+0

好吧我将压缩算法更改为JPEG,但我仍遇到同样的问题。图像不会压缩。 – Lunkie

Image image = images.get(0); 
File imgFile = new File(image.getPath()); 
if(imgFile.exists()){ 
    BitmapFactory.Options opts = new BitmapFactory.Options(); 
    opts.inSampleSize = 2; // one quarter of original size 
    profilePictureBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), opts); 
    profilePictureImage.setImageBitmap(profilePictureBitmap); 
} 

文档为inSampleSize

如果设置为值> 1,要求解码器以子采样原始图像,返回一个较小的图像以节省内存。样本大小是任一维度中与解码位图中的单个像素对应的像素数量。例如,inSampleSize == 4返回的图像是原始宽度/高度的1/4,以及像素数量的1/16。任何值< = 1的处理方式与1相同。注意:解码器使用基于2的幂的最终值,其他值将向下舍入到最近的2的幂。