Android 将旋转位图保存到SD卡后图像质量差

Android 将旋转位图保存到SD卡后图像质量差,android,Android,我正在制作一个应用程序,在其中一个活动中,我从图库中获取图像,并将其显示在下面类似适配器的图像中 我必须旋转该图像并将其保存到SD卡。我的代码做得很好,但保存到SD卡后,我得到的图像质量非常差。我的代码是: viewHolder.imgViewRotate.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { imageP

我正在制作一个应用程序,在其中一个活动中,我从图库中获取图像,并将其显示在下面类似适配器的图像中

我必须旋转该图像并将其保存到SD卡。我的代码做得很好,但保存到SD卡后,我得到的图像质量非常差。我的代码是:

 viewHolder.imgViewRotate.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            imagePosition = (Integer) v.getTag();
            Matrix matrix = new Matrix();
            matrix.postRotate(90);

            Bitmap rotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

            try {
                FileOutputStream out = new FileOutputStream(new File(uriList.get(rotatePosition).toString()));
                rotated.compress(Bitmap.CompressFormat.PNG, 100, out);
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

            notifyDataSetChanged();

        }
    });

任何建议都会大有帮助。

请尝试以下代码,在不降低图像质量的情况下减小图像大小:

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth)
{
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
    return resizedBitmap;
}
编辑:

使用
BitmapFactory
inSampleSize
选项调整图像大小,图像质量不会下降。代码:

          BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
              bmpFactoryOptions.inJustDecodeBounds = true;
              Bitmap bm = BitmapFactory.decodeFile(tempDir+"/"+photo1_path , bmpFactoryOptions);

              int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)600);
              int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)800);

              if (heightRatio > 1 || widthRatio > 1)
              {
               if (heightRatio > widthRatio){
                bmpFactoryOptions.inSampleSize = heightRatio;
               } else {
                bmpFactoryOptions.inSampleSize = widthRatio; 
               } 
              }

              bmpFactoryOptions.inJustDecodeBounds = false;
              bm = BitmapFactory.decodeFile(tempDir+"/"+photo1_path, bmpFactoryOptions);

              // recreate the new Bitmap
        src = Bitmap.createBitmap(bm, 0, 0,bm.getWidth(), bm.getHeight(), matrix, true);
        src.compress(Bitmap.CompressFormat.PNG, 100, out);

尝试将压缩比降低100到50,但仍会失真image@Raghunandan有什么建议吗?我不必减小图像的大小,我只需旋转图像并将旋转后的位图保存到SDCard,然后你的问题标题会误导他人。虽然有点效果,但静止图像有点失真。问题是
Bitmap.CompressFormat.PNG
是指文件每次旋转时都会变大。