Android画廊意图:我如何获得并设置原始裁剪图像的全尺寸和高质量而不是缩略图?

时间:2016-04-17 10:22:04

标签: android android-intent image-quality

以下是我的编辑个人资料活动,其中我向用户提供了从图库设置个人资料照片的选项。当用户点击编辑个人资料图片时,它会打开图库,裁剪所选图像,裁剪的图像将保存在预定义的位置以及临时位置,裁剪的图像将被设置为个人资料图片。但是,它总是会获得缩略图或低质量图像。图像的质量与我们通过uri将图像传递到intent中的方式相同,然后使用getData获取该图像,然后使用setImageUri设置图像,因为这样会产生缩略图而不是原始图像。这就是为什么我首先尝试保存裁剪后的图像,然后从那里获取它。即使为了避免OOM(内存不足)错误,我尝试使用inSampleSize,位图工厂选项和所有...来调整图像质量和压缩...但最后,我得到的图像质量总是相同的缩略图类型!

我想要的是标题中所说的,为userImageView设置的原始裁剪图像而不是缩略图或低分辨率图像!

还要考虑我是Android开发以及编程,编码等方面的新手!

任何形式的帮助将不胜感激!

{{1}}

2 个答案:

答案 0 :(得分:4)

查看此库是否有帮助。 https://github.com/IsseiAoki/SimpleCropView

它可以保持原始图像的质量。

修改

嗯,他们可能已经更新了库。我正在使用较旧的SimpleCropView库。这是我的实施。

用于从设备中选择图片。:

Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);

然后在活动结果:

if (requestCode == PICK_IMAGE_REQUEST && data != null && data.getData() != null) {


            try {
                String selectedImagePath = null;
                Uri selectedImageUri = data.getData();

                if (selectedImageUri.toString().contains("%")) {
                    if (selectedImageUri.toString().startsWith("content://com.google.android.apps.photos.content")) {
                        Log.v("esty", "URI: " + selectedImageUri.toString());
                        selectedImagePath = getPath.getImageUrlWithAuthority(this, selectedImageUri);

                    } else {


                        Log.v("esty", "URI: " + selectedImageUri.toString());
                        String[] UriArray = selectedImageUri.toString().split("%3A");

                        String newURIString = "content://media/external/images/media/" + UriArray[1];
                        Uri newURI = Uri.parse(newURIString);
                        Log.v("esty", "URI: " + newURI);
                        selectedImagePath = getPath.getPath(newURI, this);
                    }
                } else {
                    selectedImagePath = getPath.getPath(selectedImageUri, this);
                }

                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                bitmap1 = BitmapFactory.decodeFile(selectedImagePath, options);

                // MessageBox.Show(this, selectedImagePath);
                finalFile = new File(selectedImagePath);
                ProfilePic.setImageBitmap(bitmap1);
                filename=selectedImagePath.substring(selectedImagePath.lastIndexOf("/")+1);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

OnCreate中的SimpleCropView初始化:

CropImageView ProfilePic = (CropImageView) findViewById(R.id.cropImageView);

获取裁剪位图:

final Bitmap croppedImage = ProfilePic.getCroppedBitmap();

答案 1 :(得分:-1)

我需要做的只是稍微改变如下:

intent.putExtra("outputX", 640);
                intent.putExtra("outputY", 640);

640而不是360就完成了!我们仍然可以在xml属性中获得360 dp的宽度和高度,但是在此调整它会影响图像质量。如上所述,用640替换360,给了我想要的图像质量和比我之前更好的方式。因此,大多数图像都可以保持平均值。但是,如果此处在意图中设置的值大于原始图像,则有可能在图像周围具有黑色像素空间。因此,我们必须考虑大多数图像的平均值,并考虑图像质量。