Caching images and displaying

后端 未结 5 954
臣服心动
臣服心动 2020-11-22 15:37

Hello Am facing a particular problem in which I need to download images and display them onto a ListView corresponding to their particular TextView\'s

5条回答
  •  轮回少年
    2020-11-22 16:07

    Lazy loading using Universal Imageloader. Replace the hardcoded urls with url of images.

    Modify the below according to your requirements

    What's LazyList?. Check this link for details.

    MainActivity.java

    public class MainActivity extends Activity {
    
      private String[] mStrings={
                "http://a3.twimg.com/profile_images/670625317/aam-logo-v3-twitter.png",
                "http://a3.twimg.com/profile_images/740897825/AndroidCast-350_normal.png",
                "http://a3.twimg.com/profile_images/121630227/Droid_normal.jpg",
                "http://a1.twimg.com/profile_images/957149154/twitterhalf_normal.jpg",
                "http://a1.twimg.com/profile_images/97470808/icon_normal.png",
                "http://a3.twimg.com/profile_images/511790713/AG.png",
                "http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
                "http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
                "http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
                "http://a1.twimg.com/profile_images/349012784/android_logo_small_normal.jpg",
                "http://a1.twimg.com/profile_images/841338368/ea-twitter-icon.png",
                "http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
                "http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
                "http://a1.twimg.com/profile_images/605536070/twitterProfilePhoto_normal.jpg",
                "http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300_normal.jpg",
                "http://a1.twimg.com/profile_images/655119538/andbook.png",
                "http://a3.twimg.com/profile_images/768060227/ap4u_normal.jpg",
                "http://a1.twimg.com/profile_images/74724754/android_logo_normal.png",
                "http://a3.twimg.com/profile_images/681537837/SmallAvatarx150_normal.png",
                "http://a1.twimg.com/profile_images/63737974/2008-11-06_1637_normal.png",
                "http://a3.twimg.com/profile_images/548410609/icon_8_73.png",
                "http://a1.twimg.com/profile_images/612232882/nexusoneavatar_normal.jpg",
                "http://a1.twimg.com/profile_images/213722080/Bugdroid-phone_normal.png",
                "http://a1.twimg.com/profile_images/645523828/OT_icon_090918_android_normal.png",
                "http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
                "http://a3.twimg.com/profile_images/77641093/AndroidPlanet.png",
                "http://a1.twimg.com/profile_images/605536070/twitterProfilePhoto_normal.jpg",
                "http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300_normal.jpg",
                "http://a1.twimg.com/profile_images/655119538/andbook_normal.png",
                "http://a3.twimg.com/profile_images/511790713/AG_normal.png",
                "http://a3.twimg.com/profile_images/956404323/androinica-avatar.png",
                "http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
                "http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
                "http://a1.twimg.com/profile_images/349012784/android_logo_small_normal.jpg",
                "http://a1.twimg.com/profile_images/841338368/ea-twitter-icon_normal.png",
                "http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
                "http://a3.twimg.com/profile_images/77641093/AndroidPlanet.png",
                "http://a1.twimg.com/profile_images/605536070/twitterProfilePhoto_normal.jpg",
                "http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
                "http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
                "http://a1.twimg.com/profile_images/605536070/twitterProfilePhoto_normal.jpg",
                "http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300.jpg",
                "http://a1.twimg.com/profile_images/655119538/andbook_normal.png",
                "http://a3.twimg.com/profile_images/511790713/AG_normal.png",
                "http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
                "http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
                "http://a3.twimg.com/profile_images/121630227/Droid.jpg",
                "http://a1.twimg.com/profile_images/957149154/twitterhalf_normal.jpg",
                "http://a1.twimg.com/profile_images/97470808/icon_normal.png",
                "http://a3.twimg.com/profile_images/511790713/AG_normal.png",
                "http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
                "http://a1.twimg.com/profile_images/909231146/Android_Biz_Man.png",
                "http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
                "http://a1.twimg.com/profile_images/349012784/android_logo_small_normal.jpg",
                "http://a1.twimg.com/profile_images/841338368/ea-twitter-icon_normal.png",
                "http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
                "http://a3.twimg.com/profile_images/77641093/AndroidPlanet.png",
                "http://a3.twimg.com/profile_images/670625317/aam-logo-v3-twitter_normal.png",
                "http://a3.twimg.com/profile_images/740897825/AndroidCast-350_normal.png",
                "http://a3.twimg.com/profile_images/121630227/Droid_normal.jpg",
                "http://a1.twimg.com/profile_images/957149154/twitterhalf_normal.jpg",
                "http://a1.twimg.com/profile_images/97470808/icon.png",
                "http://a3.twimg.com/profile_images/511790713/AG_normal.png",
                "http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
                "http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
                "http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
                "http://a1.twimg.com/profile_images/349012784/android_logo_small_normal.jpg",
                "http://a1.twimg.com/profile_images/841338368/ea-twitter-icon.png",
                "http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
                "http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
                "http://a1.twimg.com/profile_images/605536070/twitterProfilePhoto_normal.jpg",
                "http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300_normal.jpg",
                "http://a1.twimg.com/profile_images/655119538/andbook_normal.png",
                "http://a3.twimg.com/profile_images/768060227/ap4u_normal.jpg",
                "http://a1.twimg.com/profile_images/74724754/android_logo.png",
                "http://a3.twimg.com/profile_images/681537837/SmallAvatarx150_normal.png",
                "http://a1.twimg.com/profile_images/63737974/2008-11-06_1637_normal.png",
                "http://a3.twimg.com/profile_images/548410609/icon_8_73_normal.png",
                "http://a1.twimg.com/profile_images/612232882/nexusoneavatar_normal.jpg",
                "http://a1.twimg.com/profile_images/213722080/Bugdroid-phone_normal.png",
                "http://a1.twimg.com/profile_images/645523828/OT_icon_090918_android.png",
                "http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
                "http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
                "http://a1.twimg.com/profile_images/605536070/twitterProfilePhoto_normal.jpg",
                "http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300_normal.jpg",
                "http://a1.twimg.com/profile_images/655119538/andbook.png",
                "http://a3.twimg.com/profile_images/511790713/AG_normal.png",
                "http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
                "http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
                "http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
                "http://a1.twimg.com/profile_images/349012784/android_logo_small_normal.jpg",
                "http://a1.twimg.com/profile_images/841338368/ea-twitter-icon.png",
                "http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
                "http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
                "http://a1.twimg.com/profile_images/605536070/twitterProfilePhoto_normal.jpg",
                "http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
                "http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
                "http://a1.twimg.com/profile_images/605536070/twitterProfilePhoto.jpg",
                "http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300_normal.jpg",
                "http://a1.twimg.com/profile_images/655119538/andbook_normal.png",
                "http://a3.twimg.com/profile_images/511790713/AG_normal.png",
                "http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
                "http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
                "http://a3.twimg.com/profile_images/121630227/Droid_normal.jpg",
                "http://a1.twimg.com/profile_images/957149154/twitterhalf.jpg",
                "http://a1.twimg.com/profile_images/97470808/icon_normal.png",
                "http://a3.twimg.com/profile_images/511790713/AG_normal.png",
                "http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
                "http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
                "http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
                "http://a1.twimg.com/profile_images/349012784/android_logo_small.jpg",
                "http://a1.twimg.com/profile_images/841338368/ea-twitter-icon_normal.png",
                "http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
                "http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png"
        };
    
        ListView lv;    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            lv= (ListView) findViewById(R.id.lv);
            lv.setAdapter(new LazyAdapter(this,mStrings));
        }
    }
    

    activity_main.xml

    
        
    
    

    LazyAdapter.java

    public class LazyAdapter extends BaseAdapter {
        private Activity activity;
        private String data[];
        private LayoutInflater inflater=null;
        public ImageLoader imageLoader; 
        DisplayImageOptions options;
        public LazyAdapter(Activity a, String[] d) {
            activity = a;
            data=d;
            inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            File cacheDir = StorageUtils.getOwnCacheDirectory(a, "MyFolderCache");
    
            // Get singletone instance of ImageLoader
            imageLoader = ImageLoader.getInstance();
            // Create configuration for ImageLoader (all options are optional)
            ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a)
                  // You can pass your own memory cache implementation
                 .discCacheExtraOptions(1024, 1024, CompressFormat.PNG, 100)
                 .discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
                 .discCacheFileNameGenerator(new HashCodeFileNameGenerator())
                 .enableLogging()
                 .build();
            // Initialize ImageLoader with created configuration. Do it once.
            imageLoader.init(config);
            //imageLoader.init(ImageLoaderConfiguration.createDefault(a));
            // imageLoader=new ImageLoader(activity.getApplicationContext());
            options = new DisplayImageOptions.Builder()
                 .showStubImage(R.drawable.ic_launcher)
                 .cacheInMemory()
                 .cacheOnDisc()
                 .displayer(new RoundedBitmapDisplayer(20))
                 .build();
        }
    
        public int getCount() {
            return data.length;
        }
    
        public Object getItem(int position) {
            return position;
        }
    
        public long getItemId(int position) {
            return position;
        }
    
        public View getView(int position, View convertView, ViewGroup parent) {
            View vi=convertView;
            ViewHolder vh = new ViewHolder();;
            if(convertView==null)
            {
    
                vi = inflater.inflate(R.layout.row, null);   
                vh.iv=(ImageView)vi.findViewById(R.id.ivv); 
                vh.pb= (ProgressBar)vi.findViewById(R.id.pb); 
                vh.tv = (TextView) vi.findViewById(R.id.textView1);
                vh.tv1= (TextView) vi.findViewById(R.id.textView2);
            }
            vh.tv.setText("Image in postion =");
            vh.tv1.setText(""+position);
            display(vh.iv, data[position], vh.pb);
            //imageLoader.displayImage(data.get(position).toString(), image,options);
            return vi;
        }
    
        public void display(ImageView img, String url, final ProgressBar spinner)
        {
            imageLoader.displayImage(url, img, options, new ImageLoadingListener() {
                @Override
                public void onLoadingStarted(String imageUri, View view) {
                    spinner.setVisibility(View.VISIBLE);
                }
                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    spinner.setVisibility(View.GONE);
                }
                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                spinner.setVisibility(View.GONE);
                }
                @Override
                public void onLoadingCancelled(String imageUri, View view) {
    
                }
            });
        }
    
        public static class ViewHolder
        {
            ImageView iv;
            TextView tv,tv1;
            ProgressBar pb;
        }
    }
    

    row.xml

    
    
        
        
        
        
    
    

    Add permission in manifest

     
    

    enter image description here

提交回复
热议问题