无法使用Android中的Universal Image Loader检索图像

时间:2014-07-04 05:31:56

标签: android universal-image-loader

我对Universal Image Loader库很新。我正在尝试将图像加载到列表视图中。这是我提到的http://cbpbenitez.blogspot.in/2013/11/universal-image-loader-tutorial.html教程。问题是没有显示图像。我的项目没有错误。我在这里发布完整的代码,请一步一步指导我。

MainActivity.class

   public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   ListView listView = (ListView)findViewById(R.id.listView);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

ImageListAdapter.class

  public class ImageListAdapter extends BaseAdapter {

private String[] urls = {
        "http://tabletpcssource.com/wp-content/uploads/2011/05/android-logo.png",
        "http://simpozia.com/pages/images/stories/windows-icon.png",
        "https://si0.twimg.com/profile_images/1135218951/gmail_profile_icon3_normal.png",
        "http://www.krify.net/wp-content/uploads/2011/09/Macromedia_Flash_dock_icon.png",
        "http://radiotray.sourceforge.net/radio.png",
        "http://www.bandwidthblog.com/wp-content/uploads/2011/11/twitter-logo.png",
        "http://weloveicons.s3.amazonaws.com/icons/100907_itunes1.png",
        "http://weloveicons.s3.amazonaws.com/icons/100929_applications.png",
        "http://www.idyllicmusic.com/index_files/get_apple-iphone.png",
        "http://www.frenchrevolutionfood.com/wp-content/uploads/2009/04/Twitter-Bird.png",
        "http://3.bp.blogspot.com/-ka5MiRGJ_S4/TdD9OoF6bmI/AAAAAAAAE8k/7ydKtptUtSg/s1600/Google_Sky%2BMaps_Android.png",
        "http://www.desiredsoft.com/images/icon_webhosting.png",
        "http://goodereader.com/apps/wp-content/uploads/downloads/thumbnails/2012/01/hi-256-0-99dda8c730196ab93c67f0659d5b8489abdeb977.png",
        "http://1.bp.blogspot.com/-mlaJ4p_3rBU/TdD9OWxN8II/AAAAAAAAE8U/xyynWwr3_4Q/s1600/antivitus_free.png",
        "http://cdn3.iconfinder.com/data/icons/transformers/computer.png",
        "http://cdn.geekwire.com/wp-content/uploads/2011/04/firefox.png?7794fe",
        "https://ssl.gstatic.com/android/market/com.rovio.angrybirdsseasons/hi-256-9-347dae230614238a639d21508ae492302340b2ba",
        "http://androidblaze.com/wp-content/uploads/2011/12/tablet-pc-256x256.jpg",
        "http://www.theblaze.com/wp-content/uploads/2011/08/Apple.png",
        "http://1.bp.blogspot.com/-y-HQwQ4Kuu0/TdD9_iKIY7I/AAAAAAAAE88/3G4xiclDZD0/s1600/Twitter_Android.png",
        "http://3.bp.blogspot.com/-nAf4IMJGpc8/TdD9OGNUHHI/AAAAAAAAE8E/VM9yU_lIgZ4/s1600/Adobe%2BReader_Android.png",
        "http://cdn.geekwire.com/wp-content/uploads/2011/05/oovoo-android.png?7794fe",
        "http://icons.iconarchive.com/icons/kocco/ndroid/128/android-market-2-icon.png",
        "http://thecustomizewindows.com/wp-content/uploads/2011/11/Nicest-Android-Live-Wallpapers.png",
        "http://c.wrzuta.pl/wm16596/a32f1a47002ab3a949afeb4f",
        "http://macprovid.vo.llnwd.net/o43/hub/media/1090/6882/01_headline_Muse.jpg"
};

private Context context;
private ImageLoader imageLoader;

public ImageListAdapter(Context context) {
    this.context = context;
    imageLoader = ImageLoader.getInstance();
}

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

@Override
public Object getItem(int arg0) {
    return null;
}

@Override
public long getItemId(int arg0) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
    View v = convertView;

    ViewHolder vh = null;
    if (v == null) {
        v = View.inflate(context, R.layout.list_item, null);

        vh = new ViewHolder();
        vh.imageView = (ImageView) v.findViewById(R.id.imageView);

        v.setTag(vh);
    }
    else {
        vh = (ViewHolder)v.getTag();
    }

    DisplayImageOptions options = new DisplayImageOptions.Builder()
    .cacheOnDisc()
    .build();

    imageLoader.displayImage(urls[position], vh.imageView, options);

    return v;
}

private class ViewHolder {
    ImageView imageView;
}

 }

UILDemoApplication.class

    public class UILDemoApplication extends Application {

@Override
public void onCreate() {
    super.onCreate();

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
    .threadPriority(Thread.NORM_PRIORITY - 2)
    .denyCacheImageMultipleSizesInMemory()
    .discCacheFileNameGenerator(new Md5FileNameGenerator())
    .tasksProcessingOrder(QueueProcessingType.LIFO)
    .enableLogging() 
    .build();

    ImageLoader.getInstance().init(config);
}

 }

权限

   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

2 个答案:

答案 0 :(得分:1)

在使用imageloader.displayimage方法之前,您必须先初始化图像加载器。

初始化图像加载器尝试此方法

private void configImageLoader() {

        // Create global configuration and initialize ImageLoader with this
        // configuration
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                getApplicationContext()).defaultDisplayImageOptions(
                getDisplayImageOptions()) // default
                .build();

        ImageLoader.getInstance().init(config);
    }

试试这个

    public class ImageListAdapter extends BaseAdapter {

    private String[] urls = {
            "http://tabletpcssource.com/wp-content/uploads/2011/05/android-logo.png",
            "http://simpozia.com/pages/images/stories/windows-icon.png",
            "https://si0.twimg.com/profile_images/1135218951/gmail_profile_icon3_normal.png",
            "http://www.krify.net/wp-content/uploads/2011/09/Macromedia_Flash_dock_icon.png",
            "http://radiotray.sourceforge.net/radio.png",
            "http://www.bandwidthblog.com/wp-content/uploads/2011/11/twitter-logo.png",
            "http://weloveicons.s3.amazonaws.com/icons/100907_itunes1.png",
            "http://weloveicons.s3.amazonaws.com/icons/100929_applications.png",
            "http://www.idyllicmusic.com/index_files/get_apple-iphone.png",
            "http://www.frenchrevolutionfood.com/wp-content/uploads/2009/04/Twitter-Bird.png",
            "http://3.bp.blogspot.com/-ka5MiRGJ_S4/TdD9OoF6bmI/AAAAAAAAE8k/7ydKtptUtSg/s1600/Google_Sky%2BMaps_Android.png",
            "http://www.desiredsoft.com/images/icon_webhosting.png",
            "http://goodereader.com/apps/wp-content/uploads/downloads/thumbnails/2012/01/hi-256-0-99dda8c730196ab93c67f0659d5b8489abdeb977.png",
            "http://1.bp.blogspot.com/-mlaJ4p_3rBU/TdD9OWxN8II/AAAAAAAAE8U/xyynWwr3_4Q/s1600/antivitus_free.png",
            "http://cdn3.iconfinder.com/data/icons/transformers/computer.png",
            "http://cdn.geekwire.com/wp-content/uploads/2011/04/firefox.png?7794fe",
            "https://ssl.gstatic.com/android/market/com.rovio.angrybirdsseasons/hi-256-9-347dae230614238a639d21508ae492302340b2ba",
            "http://androidblaze.com/wp-content/uploads/2011/12/tablet-pc-256x256.jpg",
            "http://www.theblaze.com/wp-content/uploads/2011/08/Apple.png",
            "http://1.bp.blogspot.com/-y-HQwQ4Kuu0/TdD9_iKIY7I/AAAAAAAAE88/3G4xiclDZD0/s1600/Twitter_Android.png",
            "http://3.bp.blogspot.com/-nAf4IMJGpc8/TdD9OGNUHHI/AAAAAAAAE8E/VM9yU_lIgZ4/s1600/Adobe%2BReader_Android.png",
            "http://cdn.geekwire.com/wp-content/uploads/2011/05/oovoo-android.png?7794fe",
            "http://icons.iconarchive.com/icons/kocco/ndroid/128/android-market-2-icon.png",
            "http://thecustomizewindows.com/wp-content/uploads/2011/11/Nicest-Android-Live-Wallpapers.png",
            "http://c.wrzuta.pl/wm16596/a32f1a47002ab3a949afeb4f",
            "http://macprovid.vo.llnwd.net/o43/hub/media/1090/6882/01_headline_Muse.jpg"
    };

    private Context context;
    private ImageLoader imageLoader;

    public ImageListAdapter(Context context) {
        this.context = context;
        configImageLoader();
        imageLoader = ImageLoader.getInstance();
    }

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

    @Override
    public Object getItem(int arg0) {
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        View v = convertView;

        ViewHolder vh = null;
        if (v == null) {
            v = View.inflate(context, R.layout.list_item, null);

            vh = new ViewHolder();
            vh.imageView = (ImageView) v.findViewById(R.id.imageView);

            v.setTag(vh);
        }
        else {
            vh = (ViewHolder)v.getTag();
        }

        imageLoader.displayImage(urls[position], vh.imageView, options);

        return v;
    }

 private void configImageLoader() {

            // Create global configuration and initialize ImageLoader with this
            // configuration
            ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                    getApplicationContext()).defaultDisplayImageOptions(
                    getDisplayImageOptions()) // default
                    .build();

            ImageLoader.getInstance().init(config);
        }

    private class ViewHolder {
        ImageView imageView;
    }

     }

答案 1 :(得分:0)

您的代码中存在两个问题:

  1. 您已创建ImageLoader的实例但尚未初始化ImageLoader。为此,在imageLoader = ImageLoader.getInstance()之后在构造函数中添加这行代码; :

    imageLoader.init(ImageLoaderConfiguration.createDefault(CTX));

  2. 您尚未为imageLoader声明任何选项。修改DisplayImageOptions代码,如下所示:

    DisplayImageOptions options = new DisplayImageOptions.Builder()         .showImageOnLoading(R.drawable.image_loading)         .showImageForEmptyUri(R.drawable.no_image)         .showImageOnFail(R.drawable.image_failed)         .cacheInMemory(真)         .cacheOnDisc(真)         .bitmapConfig(Bitmap.Config.RGB_565)         .build();