4

I'm getting all the images from my gallery.(pictures from Samsung galaxy s5)
I can view the images from Viewpager.
But when I start Swiping. Its too Slow.
And after a few seconds, the app crashed without having error in logcat.

public class Paging extends PagerAdapter {
        private ArrayList<String> IMAGES = new ArrayList<>();
        private Context context;
        private LayoutInflater layoutInflater;
        public Paging(Context context) {
            this.context = context;
        }

        @Override
        public int getCount() {
            return MainActivity.IMAGES.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return (view == (LinearLayout) object);
        }

        @Override
        public View instantiateItem(View container, int position) {
            IMAGES = (ArrayList<String>) MainActivity.IMAGES.clone();



            layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View item_view = layoutInflater.inflate(R.layout.imagepager_layout, (ViewGroup) container, false);
            ImageView imageView = (ImageView) item_view.findViewById(R.id.img);


            Picasso.with(context)
                    .load(IMAGES.get(position))
                    .placeholder(R.drawable.plusbtn)
                    .into(imageView);

        ((ViewGroup) container).addView(item_view);

            return item_view;


        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((LinearLayout) object);
        }
    }

My viewPager adding adapter.

 viewPager = (ViewPager)findViewById(R.id.view_pager);
 adap = new Paging(MainActivity.this);
 viewPager.setAdapter(adap);

My XMLs..

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:gravity="center" />
</LinearLayout>

My view Pager.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:orientation="vertical"
    >

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></android.support.v4.view.ViewPager>
</LinearLayout>
Charles Galvez
  • 1,100
  • 5
  • 19
  • 41
  • Your image is too large so application will occur ANR. Try to decrease image size before load it. – Neo Nov 09 '15 at 03:11
  • how can i decrease the size of image but will be the same as viewing in my phone gallery? – Charles Galvez Nov 09 '15 at 03:12
  • Google have many answer about "Reduce image size in android". Here's an example to scale image before loading: http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object/823966#823966 – Neo Nov 09 '15 at 03:15
  • Or using `resize` function in `Picasso` lib: http://www.101apps.co.za/index.php/articles/gridview-tutorial-using-the-picasso-library.html – Neo Nov 09 '15 at 03:21
  • I used the resiez function of picasso but some images which is portrait is became small like its landscape. – Charles Galvez Nov 09 '15 at 03:23
  • Just want to add that with the Fresco image loading library there is `setDownsampleEnabled()` method which automatically samples images at the correct size. – Breavyn Nov 09 '15 at 05:06

2 Answers2

3

this is most probably caused by loading too much image pixel data than necessary (to fit your view size). if your images are huge in size, Picasso by default would load full size of the image into memory, leading to both slow frame rate as well as OOM exceptions.

a quick fix would be to use Picasso.with().load().resize(width, height). It will enable Picasso to load a downsampled version of the image into memory. on the UI side, you could use fitCenter scale type for your ImageView.

to understand the concept better, though, i would suggest reading more on Google's developer guide on this topic here: Loading Large Bitmaps Efficiently

bosphere
  • 388
  • 3
  • 7
  • tried to use resize, but some portrait pictures which is full screen became so small.. – Charles Galvez Nov 09 '15 at 04:41
  • try adding this attribute to your `` in xml: `android:scaleType="fitCenter"`, this would scale up your resized image to fit the view size. also consider trying out different target width/height when resizing. – bosphere Nov 10 '15 at 08:02
-1

1.Picasso set mode to RGB565,set Memory cache and Disc Cache.

2.Reuse view in ViewPager.

3.ViewPager setPageoffset(1);

4.try:

<application
    android:largeHeap="true"/>
tiny sunlight
  • 6,231
  • 3
  • 21
  • 42