ImageView最初没有显示图像

时间:2014-10-05 22:30:09

标签: android gridview android-viewpager android-volley touchimageview

我有GridView,它显示少量图像和一个与TouchImageView集成的ViewPager。除了一个场景外,似乎工作正常。

当您单击任何GridView的缩略图图像以启动ViewPager时,response.getBitMap将显示为NULL。但是当我们继续向右滑动时,图像加载正常。当我们向后滑动时,甚至会显示第一张图像。每次从GridViewActivity点击GridView的缩略图时都会发生这种情况。

检查日志文件后,我可以看到ImageLoader一次请求2个图像(当前和下一个图像),但是对于初始加载,它执行相同的操作两次(因此总共4个请求,请检查下面的logcat)。

FullScreenImageAdapter.java代码:

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

不确定为什么会这样。我能做些什么来解决它?

1 个答案:

答案 0 :(得分:0)

通过以下方式加载图片解决了问题:

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