0

我有显示少量图像的 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);
}
}

日志猫

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

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

4

1 回答 1

0

通过以下方式加载图像修复了该问题:

//Loading image with placeholder and error image
imageLoader.get(Const.URL_IMAGE, ImageLoader.getImageListener(
            imageView, R.drawable.ico_loading, R.drawable.ico_error));
于 2014-10-07T13:16:09.883 回答