1

How can i resize image before loading to imageview after selecting from gallery/photos?. Otherwise large images are causing OOM issues.

SelectImageGallery.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 Image From Gallery"), 1);
    }
}
Uri uri = I.getData();
    try {
        bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
        imageView.setImageBitmap(bitmap);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Mahmoud Abu Elheja
  • 1,847
  • 1
  • 20
  • 28

4 Answers4

0

Try:

Bitmap resized_Bitmap = Bitmap.createScaledBitmap(yourBitmap, newWidth, newHeight, true);
imageView.setImageBitmap( resized_Bitmap);
anehme
  • 416
  • 3
  • 16
0

refer to this answer

you can resize your bitmap like this

import android.graphics.Matrix

public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // CREATE A MATRIX FOR THE MANIPULATION
    Matrix matrix = new Matrix();
    // RESIZE THE BIT MAP
    matrix.postScale(scaleWidth, scaleHeight);

    // "RECREATE" THE NEW BITMAP
    Bitmap resizedBitmap = Bitmap.createBitmap(
        bm, 0, 0, width, height, matrix, false);
    bm.recycle();
    return resizedBitmap;
}
Mahmoud Abu Elheja
  • 1,847
  • 1
  • 20
  • 28
0

As you facing OOM issue, you can remove this issue by

1) Decrease size of bitmap and

2) Increase size of app by changing in gradle.properties and AndroidManifest file

gradle.properties org.gradle.jvmargs=-Xmx1536m

Android Manifest android:largeHeap="true", android:hardwareAccelerated="true"

webaddicted
  • 592
  • 5
  • 17
  • Thanks @webaddicted i understand that android:largeHeap="true", and android:hardwareAccelerated="true" have other issues like slowing down the app. I tried it though didn't work at some point share – shinsky Paul Jan 13 '20 at 18:35
0

I finally made it to resolve it using glide as follows for those who might need it in future. Selecting Intent

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);
    }
}

Setting Image to Imageview Using Glide

    @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) {
                        }
                    });
        }