androidgridviewandroid-viewpagerandroid-volleytouchimageview

ImageView not showing image initially


I have GridView which displays few images and a ViewPager integrated with TouchImageView. It seems to be working fine except for one scenario.

When you click on any GridView's thumbnail image to initiate the ViewPager, the response.getBitMap is coming up as NULL. But when we keep swiping to right, images are loading fine. Even the first image is displayed when we swipe back to it. It happens everytime when an GridView's thumbnail image is clicked initially from GridViewActivity.

Upon checking the log files, I can see ImageLoader is making request for 2 images at a time (current and the next image) but for initial load it doing the same thing twice (so total 4 requests, check logcat below).

FullScreenImageAdapter.java code:

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class FullScreenImageAdapter extends PagerAdapter {
//Log tag
private static final String TAG = FullImageActivity.class.getSimpleName();
private Activity _activity;
private String[] _imagePaths, _imageCaptions;
private LayoutInflater inflater;
private View mCurrentView;
TouchImageView imgDisplay;

// constructor
public FullScreenImageAdapter(Activity activity, String[] imagePaths, String[] imageCaptions) {
    this._activity = activity;
    this._imagePaths = imagePaths;
    this._imageCaptions = imageCaptions;
}

@Override
public int getCount() {
    return this._imagePaths.length;
}

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

public String getImageCaption(int position) {
    return _imageCaptions[position];
}

@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
    mCurrentView = (View)object;
}

public View getCurrentView() {
    return mCurrentView;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {

    inflater = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image, container, false);

    imgDisplay = (TouchImageView) viewLayout.findViewById(R.id.imgDisplay);

    // We download image into ImageView
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();
    String strFullImageURL = _imagePaths[position];

    imageLoader.get(strFullImageURL, new ImageLoader.ImageListener() {
        @Override
        public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {

            Log.d(TAG, "Outside IF response.getBitmap() = "+response.getBitmap()+"");
            if (response.getBitmap() != null) {
                Log.d(TAG, "Inside IF response.getBitmap() = "+response.getBitmap()+"");
                // load bitmap into imageview
                imgDisplay.setImageBitmap(response.getBitmap());
            }
        }

        @Override
        public void onErrorResponse(VolleyError error) {
            // unable to fetch full image
            Log.d(TAG, "VolleyError while downloading full image: "+error.getMessage());
        }
    });

    ((ViewPager) container).addView(viewLayout);

    return viewLayout;
}

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

Logcat

10-05 18:16:31.286  24976-24976/com.xyz D/GridViewActivity﹕ Array length: 32
10-05 18:16:31.626  24976-24976/com.xyz D/FullImageActivity﹕ Outside IF response.getBitmap() = null
10-05 18:16:32.277  24976-24976/com.xyz D/FullImageActivity﹕ Outside IF response.getBitmap() = android.graphics.Bitmap@42a4f4a0
10-05 18:16:32.277  24976-24976/com.xyz D/FullImageActivity﹕ Inside IF response.getBitmap() = android.graphics.Bitmap@42a4f4a0
10-05 18:16:32.477  24976-24976/com.xyz D/FullImageActivity﹕ Outside IF response.getBitmap() = android.graphics.Bitmap@42a737d0
10-05 18:16:32.477  24976-24976/com.xyz D/FullImageActivity﹕ Inside IF response.getBitmap() = android.graphics.Bitmap@42a737d0

Not sure why this is happening. Anything I could do to fix it?


Solution

  • Fixed the issue by loading the image in below way:

    //Loading image with placeholder and error image
    imageLoader.get(Const.URL_IMAGE, ImageLoader.getImageListener(
                imageView, R.drawable.ico_loading, R.drawable.ico_error));