有谁知道如何缩放位图图像而不会失去图像质量?目前我面临这个问题,所选图像的大小可能太大导致应用程序返回到另一个活动.
所以现在我尝试使用这种方法来缩放所选图像而不会降低其质量.我从这里得到了代码.
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; }
答案似乎对大多数人有帮助,但为什么它对我不起作用?我错过了什么吗?
原始图像
在imageView上选择图像显示