gpt4 book ai didi

android - 无法在 Android 中使用 Universal Image Loader 检索图像

转载 作者:行者123 更新时间:2023-11-30 02:48:50 25 4
gpt4 key购买 nike

我对 Universal Image Loader 库还很陌生。我正在尝试将图像加载到 ListView 中。这是我引用的教程 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"/>

最佳答案

在使用 imageloader.displayimage 方法之前,您必须先初始化 imageloader。

初始化图像加载器试试这个方法

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;
}

}

关于android - 无法在 Android 中使用 Universal Image Loader 检索图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24567280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com