How to resize image before loading to ImageView to avoid OOM issues

后端 未结 4 1588
忘了有多久
忘了有多久 2021-01-24 04:18

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

SelectImageGallery.setOnC         


        
4条回答
  •  -上瘾入骨i
    2021-01-24 05:02

    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(512, 512) {
                            @Override
                            public void onResourceReady(@NonNull Bitmap bitmap1, @Nullable Transition transition) {
                                imageView1.setImageBitmap(bitmap1);
                                MainActivity.this.bitmap1 = bitmap1;
                            }
    
                            @Override
                            public void onLoadCleared(@Nullable Drawable placeholder) {
                            }
                        });
            }
    

提交回复
热议问题