Android-加载到ImageView之前调整图像大小,以避免OOM问题

时间:2020-01-11 10:05:53

标签: java android

从图库/照片中选择后,如何缩小图像视图上的图像尺寸?否则,选择的大图像会导致OOM问题。

选择意图

SelectImageGallery1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Image1 From Gallery"), 1);
    }
}

设置为ImageView

{
    Uri uri = I.getData();
    try {
        bitmap1 = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
        imageView1.setImageBitmap(bitmap1);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

3 个答案:

答案 0 :(得分:0)

为此目的使用毕加索

implementation 'com.squareup.picasso:picasso:2.71828'

要加载图像,请使用此代码

Picasso.get()
    .load(R.drawable.landing_screen)
    .resize(50, 50)
    .into(imageView1);

有关更多信息,请访问链接:

https://square.github.io/picasso/

您还可以使用它来从Web异步加载图像,而无需您自己的任何HTTP请求。

答案 1 :(得分:0)

请查看文档的Loading Large Bitmaps Efficiently部分,特别是Load a Scaled Down Version into Memoryhttps://developer.android.com/topic/performance/graphics/load-bitmap#load-bitmap

通过设置inSampleSize,可以控制Bitmap的大小,然后将其加载。

从文档中

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) >= reqHeight
                && (halfWidth / inSampleSize) >= reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

文档以资源为例,但是您应该能够通过抓住ContentResolver并使用InputStream来使用BitmapFactory.decodeStream(...)

ContentResolver cr = getApplicationContext().getContentResolver();
InputStream is = cr.openInputStream(uri);

答案 2 :(得分:0)

对于那些将来可能需要它的人,我终于使用滑行解决了它。 选择意图

SelectImageGallery1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Image1 From Gallery"), 1);
    }
}

使用Glide将图像设置为Imageview

    @Override
    protected void onActivityResult(int RC, int RQC, Intent I) {
        super.onActivityResult(RC, RQC, I);
        if (RC == 1 && RQC == RESULT_OK && I != null && I.getData() != null) {
            Uri uri = I.getData();
            RequestOptions options = new RequestOptions()
                    .format(DecodeFormat.PREFER_RGB_565)
                    .placeholder(R.drawable.ic_launcher_background)
                    .error(R.drawable.ic_launcher_background);

            Glide.with(this)
                    .setDefaultRequestOptions(options)
                    .asBitmap()
                    .load(uri)
                    .centerInside()
                    .into(new CustomTarget<Bitmap>(512, 512) {
                        @Override
                        public void onResourceReady(@NonNull Bitmap bitmap1, @Nullable Transition<? super Bitmap> transition) {
                            imageView1.setImageBitmap(bitmap1);
                            MainActivity.this.bitmap1 = bitmap1;
                        }

                        @Override
                        public void onLoadCleared(@Nullable Drawable placeholder) {
                        }
                    });
        }