0

I am using ViewPager in my app and all the images are coming from server. All thing works fine, but Images are taking so much time to display on screen, can any one tell what is issue, following is my snippet code. Or is there any efficient way to load images?

public class FullImage extends Activity{

private String strtd;
String[] imgStr;
ImageView imageView;
ArrayList<String> userImgArrayList;
String[] myURLs;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.full_image_view);

    //strtd = getIntent().getStringExtra("profile_pic");
    userImgArrayList = getIntent().getStringArrayListExtra("user_images");


    ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
       ImageAdapter adapter = new ImageAdapter(this);
       viewPager.setAdapter(adapter);

     imageView = (ImageView) findViewById(R.id.full_image_view);

   }

       public Bitmap getResizedBitmap(Bitmap image, int bitmapWidth,
        int bitmapHeight) {
        return Bitmap.createScaledBitmap(image, bitmapWidth, bitmapHeight,
            true);
   }
public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
    int width = image.getWidth();
    int height = image.getHeight();

    float bitmapRatio = (float)width / (float) height;
    if (bitmapRatio > 0) {
        width = maxSize;
        height = (int) (width / bitmapRatio);
    } else {
        height = maxSize;
        width = (int) (height * bitmapRatio);
    }
    return Bitmap.createScaledBitmap(image, width, height, true);
    }
         public class ImageAdapter extends PagerAdapter {
     Context context;
      ImageAdapter(Context context)
     {
     this.context=context;
     }
      @Override
      public int getCount() {
      return userImgArrayList.size();
      }

      @Override
        public void destroyItem(View container, int position, Object object) {
             ((ViewPager) container).removeView((View) object);
        }
     @Override
     public boolean isViewFromObject(View view, Object object) {
     return view == ((ImageView) object);
     }

     @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = new ImageView(context);
            int padding = context.getResources().getDimensionPixelSize(
                    R.dimen.activity_horizontal_margin);
            imageView.setPadding(padding, padding, padding, padding);
            //imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            Picasso.with(context).load(userImgArrayList.get(position)).into(imageView);

            /*for(int i=0; i<myURLs.length;i++)
            { 

                try {
                    url = new URL(myURLs[i]);
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                try {
                    bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                 imageView.setImageBitmap(bmp);
            }
            */
            // imageView.setImageURI(Uri.parse(imgStr[position]));
            ((ViewPager) container).addView(imageView, 0);
            return imageView;
        }
     }
  • what is the size of images in the url(is it high resolution) – George Thomas Apr 14 '15 at 05:20
  • yes it is high resolution..and as per my app user can upload any size of image..so what i need user upload any size,but view pager should not load –  Apr 14 '15 at 05:25
  • If its high resolution it will take some time according to your connection,try resize option in Picasso may be it will reduce the loading time a bit. And if you dont want to load the images each time the view pager comes then you could cache the images, so it will only load for one time – George Thomas Apr 14 '15 at 05:32
  • can you tell how to do this? –  Apr 14 '15 at 05:38

2 Answers2

0

I dont know whether the resizing images will solve your problem anyway give it a try

x,y are width and height you need for the image

Picasso.with(context).load(userImgArrayList.get(position)).resize(x, y) .into(imageView);

According to me Universal image loader is much better its a little pain in setting up the universal image loader but i think it has a better image caching facility You can look into a example here Universal image loader sample

If i would say you can resize the image at the server side, so resize all images uploaded by the user to a size without losing the aspect ratio, so most images will load more smoothly

George Thomas
  • 4,566
  • 5
  • 30
  • 65
  • yup it resize and and loading time also reduced..but what if user will upload camera image??? –  Apr 14 '15 at 06:38
  • i gave x and y as 200 200 –  Apr 14 '15 at 06:38
  • ya now if you resize all the images uploaded to the server to a particular size with correct aspect ratio then it will be lot more faster – George Thomas Apr 14 '15 at 07:52
  • can you tell one more thing? as per this code see that I am getting intent from previous page..but what if want to add pager in preious page from where I am getting intent?? –  Apr 14 '15 at 09:43
  • sorry i didn't understand – George Thomas Apr 18 '15 at 04:39
  • nothing issue is solved but can you help ?http://stackoverflow.com/questions/29699390/how-to-set-selected-image-from-gallery-in-gridview/29699591?noredirect=1#comment47537463_29699591 –  Apr 18 '15 at 04:51
0

Please go through this Google's official information, may be it will help you: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

And here is another question which is explained very well: Android - Efficient way to load multiple images from remote server

Sorry for lots of links but these have sufficient information: http://android-vogue.blogspot.jp/2013/01/loading-large-bitmaps-efficiently-in.html

Community
  • 1
  • 1
Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
  • image loading issue is solved but I am trying to add view pager in single activity instead of sending arraylist to next activity..check my question –  Apr 14 '15 at 10:56