Search code examples
androidandroid-recyclerviewandroid-camerascrollview

Scroll-view not scrolling properly when add images on Recycler-View in android


Hi I have inserted Recycler-View inside scroll-view when i added multiple images inside Recycler-View then scroll view is not scrolling properly and after some time it's crashing automatically and i think it's images high resolution problem can some one help me please

code:-

ContentValues values = new ContentValues();
                values.put(MediaStore.Images.Media.TITLE, "New Picture");
                values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera");
                imageUri = getActivity().getContentResolver().insert(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(intent, Constants.CAMERA_REQUEST_CODE);



 thumbnail = Bitmap.createScaledBitmap(BitmapFactory.decodeFile("" + shopImageFile),
                        100, 100, true);
                shop_image.setImageBitmap(thumbnail);
                Utilities.setEmptyError(business_name_layout);
                setImageMargins(shop_image);

xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"
    android:fillViewport="true">


                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recyclerView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:nestedScrollingEnabled="false"
                    android:layout_marginLeft="16dp"
                    android:layout_marginTop="3dp" />


</Scrollview>

Solution

  • Always do complex operations on worker thread or use AsyncTask as in

    In your Adapter add below class

    static class ImageLoader extends AsyncTask<>{
      private ImageView shop_image;
       ImageLoader(ImageView shop_image){
          this.shop_image = shop_image;
        }
     ..
       public Bitmap doInBackground(){
              return Bitmap.createScaledBitmap(BitmapFactory.decodeFile("" + shopImageFile),
                            100, 100, true);
    
       }
    
       public void onPostExecute(Bitmap thumbnail){
         shop_image.setImageBitmap(thumbnail);
                    Utilities.setEmptyError(business_name_layout);
                    setImageMargins(shop_image);
        }
     ..
    }
    

    In Bind Holder method

    new ImageLoader().execute();