有谁知道如何在不损失图像质量的情况下缩放位图图像?目前我面临这个problem https://stackoverflow.com/questions/34315387/will-the-image-too-big-caused-app-return-to-previous-activity,其中所选图像的尺寸可能太大导致应用程序返回到另一个活动。
所以现在我尝试使用这种方法来缩放所选图像而不损失其质量。我从以下位置获取代码here http://adilatwork.blogspot.my/2012/10/android-how-to-scale-bitmap-and-retain.html.
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_LOAD_IMAGE:
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK & null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver()
.query(selectedImage, filePathColumn, null, null,
null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap a = (BitmapFactory.decodeFile(picturePath));
photo = scaleBitmap(a, 200, 200);
imageView.setImageBitmap(photo);
}
break;
}
public static Bitmap scaleBitmap(Bitmap bitmap, int newWidth, int newHeight) {
Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);
float scaleX = newWidth / (float) bitmap.getWidth();
float scaleY = newHeight / (float) bitmap.getHeight();
float pivotX = 0;
float pivotY = 0;
Matrix scaleMatrix = new Matrix();
scaleMatrix.setScale(scaleX, scaleY, pivotX, pivotY);
Canvas canvas = new Canvas(scaledBitmap);
canvas.setMatrix(scaleMatrix);
canvas.drawBitmap(bitmap, 0, 0, new Paint(Paint.FILTER_BITMAP_FLAG));
return scaledBitmap;
}
答案似乎对大多数人都有帮助,但为什么它对我不起作用?我错过了什么吗?
原始图像 https://i.stack.imgur.com/BaGsn.jpg
选定的图像显示在 imageView 上 https://i.stack.imgur.com/ZGAuZ.jpg