Does anyone know how to scale a bitmap without losing image quality? I am currently facing this problem where the size of the selected image, perhaps too large, made the application return to another action.
So, now I tried to use this method to scale the selected image without losing its quality. I get the code here .
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; }
The answer seems useful to most people, but why doesnβt this work for me? Did I miss something?
source image
selected image image in imageView
user5256621
source share