缓存图片并显示

16

你好,我遇到了一个问题,需要下载图片并将其显示在与它们对应的TextViewListView上。我的代码成功地显示了我需要显示的TextView,但我不知道如何在ListView中显示所有这些不同的图片。

在SO上阅读了很多帖子之后,得出以下两种解决方法: 1. 懒加载列表 2. 通用图像加载器

我已经尝试了这两个解决方案。我下载了懒加载列表代码,其中URL是硬编码字符串存储在数组中的。我想自己动态创建字符串,将它们存储到缓存中,并显示所有相应的图像。

以下是我的代码:

public class Tools_ListItemActivity extends ListActivity 
{
    private Context context;
    String s;

    private static final String TAG_POSTS = "posts";
    private static final String TAG_MDNAME = "mdname";
    private static final String TAG_UTCOST = "utcost";
    private static final String TAG_IIMG= "iimg";
    JSONArray posts = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        s=getIntent().getExtras().getString("url");
        new ProgressTask(Tools_ListItemActivity.this).execute();
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
    }

ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();

     ListView lv ;



      private class ProgressTask extends AsyncTask<String, Void, Boolean> {
      private ProgressDialog dialog;

       public ProgressTask(ListActivity activity) {

       Log.i("1", "Called");
       context = activity;
       dialog = new ProgressDialog(context);
      }

       /** progress dialog to show user that the backup is processing. */

       /** application context. */
      private Context context;

       protected void onPreExecute() {
       this.dialog.setMessage("Progress start");
       this.dialog.show();
      }

       @Override
      protected void onPostExecute(final Boolean success) {
       if (dialog.isShowing()) {
        dialog.dismiss();
       }
       ListAdapter adapter = new SimpleAdapter(context, jsonlist,
         R.layout.activity_toolsitem, new String[] { TAG_IIMG, TAG_MDNAME, TAG_UTCOST  }, new int[] {
           R.id.imageViewUrl, R.id.mdname, R.id.utcost });

        setListAdapter(adapter);

        // selecting single ListView item
        lv = getListView();

      }

       protected Boolean doInBackground(final String... args) {

        JSONParser jParser = new JSONParser();

        // getting JSON string from URL
        JSONObject json = jParser.getJSONFromUrl(s);


        try {
            posts = json.getJSONArray(TAG_POSTS);
        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try
        {
        // looping through All Contacts
        for(int i = 0; i < posts.length(); i++){
            JSONObject c = posts.getJSONObject(i);

            // Storing each json item in variable
            String mdname = c.getString(TAG_MDNAME);
            String utcost= c.getString(TAG_UTCOST);
            String iimg=c.getString(TAG_IIMG);

            //Forming the Url of the image to be shown in the list view
            String imageUrl="My_App_URL"+iimg;

/*  try {

              String imageUrl="My_App_URL"+iimg;
              ImageView imageView = (ImageView)findViewById(R.id.imageViewUrl);
              Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());
              imageView.setImageBitmap(bitmap); 
            } catch (MalformedURLException e) {
              e.printStackTrace();
            } catch (IOException e) {
              e.printStackTrace();
            } */


            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();

            // adding each child node to HashMap key => value
            map.put(TAG_MDNAME, mdname);
            map.put(TAG_UTCOST, utcost);
            map.put(TAG_IIMG, iimg);



         jsonlist.add(map);
        } }catch (JSONException e) 
        {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }


        return null;


       }

      }





}

我已经进行了调试并发现imageUrl正确形成。 Json只返回jpg的名称,因此我将其附加到URL中并存储在imageUrl中。另外两个文本视图正在被正确解析和显示。如果有人能帮助我在图像视图中显示图像,那就太好了。谢谢。

更新: 在Pankaj分享的链接(https://github.com/AndroidBegin/Android-JSON-Parse-Images-and-Texts-Tutorial)和下面Raghunandan分享的概念知识的帮助下,我已经成功解决了我的特定问题。请查看这些答案以获取详细说明和延迟加载的实现。由于我离开了两天,无法用赏金奖励一个答案,深感抱歉。


1
https://dev59.com/6WUo5IYBdhLWcg3w-zei. 可能会有所帮助。 - Raghunandan
@D'yerMak'er 如果你想提高接受率,你应该接受下面Raghunandan的答案,这正是你所需要的。同时不要浪费你的悬赏,他们值得得到它。 - Prateek
@prateek:我还没有检查下面给出的答案。我会先检查它们,如果找到了我要找的内容,我肯定会奖励悬赏金。而且是在时间用尽之前完成的。不用担心。 - D'yer Mak'er
我不知道这些答案中的任何一个比其他答案更好。它们都可以工作。只是做同样事情的不同方式。如果您的代码有更具体的问题,而不是一般性的更好方法,也许我们可以提供帮助。 - Rarw
@Rarw:Raghunandan和pankaj的答案共同帮助我解决了问题。在JSON解析时动态形成URL是一个问题。 - D'yer Mak'er
显示剩余2条评论
5个回答

17

使用Universal Imageloader实现懒加载。将硬编码的图片url替换为实际的图片url。

根据您的需求修改以下内容

LazyList是什么?点击此链接获取详细信息。

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

主活动界面.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <ListView
        android:id="@+id/lv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#000000"
        android:focusableInTouchMode="false"
        android:listSelector="@android:color/transparent"
        android:layout_weight="2.0"
        android:divider="#000000"
        android:headerDividersEnabled="false"
        android:footerDividersEnabled="false"
        android:dividerHeight="8dp"
        android:drawSelectorOnTop="false"
        />
</RelativeLayout>

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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_launcher" />
    <ProgressBar 
        android:id="@+id/pb"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="21dp"
        android:layout_marginLeft="31dp"
        android:text="TextView" />
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignParentBottom="true"
        android:text="TextView" />
</RelativeLayout>
在清单文件中添加权限。
 <uses-permission android:name="android.permission.INTERNET"/>

enter image description here


如果是那样的话,我猜他无论如何也不会问这个问题。 - Raghunandan
@Raghunandan:抱歉回复晚了。我的问题是,我正在处理来自服务器的许多URL字符串作为JSON。我必须形成这些URL,然后继续下载任务。您提供的解决方案对于硬编码字符串URL非常有效,但在我的情况下不太适用。在尝试找出不同方法之后,我发布了这个问题。现在,Pankaj Sharma下面提出的解决方案帮助我形成了答案。所以我将接受那个答案。但是感谢您解释懒惰列表的概念和使懒惰列表工作所需的类。非常感谢。 - D'yer Mak'er
@D'yerMak'er 你需要解析JSON并将其存储到列表中,并在自定义适配器中提供URL。 这很简单,但你需要修改相同的内容。 上述方法适用于硬编码和从解析JSON获取的URL。 我已经测试过两种情况,并在我的应用程序中使用了它们。 - Raghunandan
@D'yerMak'er 好的,我把决定最有帮助的答案留给你。但是我可以说上面的方法很好用。你只需要在解析JSON后提供URL即可。 - Raghunandan
@Raghunandan:仔细想想,你的答案对于未来可能需要此解决方案的人更完整,因此我接受了你的答案。非常感谢你的回复,它帮了我很大的忙。 - D'yer Mak'er
显示剩余6条评论

3
请尝试使用通用图片加载器进行此操作。
public class DetailsListViewAdapter extends ArrayAdapter<Entry>{
    private Context context;
    private List<Entry> detailList;
    DisplayImageOptions options;
    public ImageLoader imageLoader;

    public DetailsListViewAdapter(Context context, int textViewResourceId,List<Entry> detailList) {
        super(context, textViewResourceId,detailList);
        this.detailList = detailList;
        this.context = context;
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        options = new DisplayImageOptions.Builder()
        .showImageForEmptyUri(R.drawable.blankimage)
        .showImageOnFail(R.drawable.blankimage)
        .resetViewBeforeLoading()
        .cacheOnDisc()
        .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
        .bitmapConfig(Bitmap.Config.RGB_565)
        .displayer(new FadeInBitmapDisplayer(300))
        .build();
    }

    static class ViewHolder{
        private TextView popupDetails;
        private TextView pubDate;
        private ImageView image;
        private ProgressBar progress;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        ViewHolder holder;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.details_list_item, null);
            holder = new ViewHolder();
            holder.popupDetails=(TextView)v.findViewById(R.id.detailHeading);
            holder.pubDate = (TextView)v.findViewById(R.id.pubDate);
            holder.image = (ImageView)v.findViewById(R.id.listImg);
            holder.progress = (ProgressBar)v.findViewById(R.id.progressBar);
            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }
        Entry value = detailList.get(position);
        holder.popupDetails.setText(value.getTitle());
        holder.pubDate.setText(value.getPubDate());
        String url = value.getImageLink();
        try {
            if(url.length() > 5) {
                holder.image.setVisibility(View.VISIBLE);
                loadImage(holder.image,url,holder.progress);
            } else {
                if(url.equals("NO")) {
                    holder.image.setVisibility(View.INVISIBLE);
                } else {
                    holder.image.setVisibility(View.GONE);
                }
            }
        } catch (Exception e) {
            Logger.show(e);
        }
        v.setBackgroundColor(Color.WHITE);
        return v;

    }
    public void loadImage(ImageView imageView, String loadURL,final ProgressBar progress) {
        imageLoader.displayImage(loadURL, imageView, options, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {
                progress.setVisibility(View.VISIBLE);
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                String message = null;
                switch (failReason) {
                case IO_ERROR:
                    message = "Input/Output error";
                    break;
                case OUT_OF_MEMORY:
                    message = "Out Of Memory error";
                    break;
                case NETWORK_DENIED:
                    message = "Downloads are denied";
                    break;
                case UNSUPPORTED_URI_SCHEME:
                    message = "Unsupported URI scheme";
                    break;
                case UNKNOWN:
                    message = "Unknown error";
                    break;
                }
                Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onLoadingComplete(final String imageUri, View view, final Bitmap loadedImage) {
                progress.setVisibility(View.INVISIBLE);
            }  
        });
    }
}

我已经尝试过这个方法:请参见此处http://stackoverflow.com/questions/16861807/is-the-listview-recycling-early-or-is-universal-imageloader-canceling-in-error/16972610#16972610 listView会在图片加载之前回收第一个视图,因此加载将被取消,并且第一项上的图片将不会显示。 - WIllJBD

2
以下是我在创建用户联系人列表时,如何加载和缓存图片的方法。左侧是个人资料图片,右侧是一些文本视图(这听起来与您正在处理的问题很接近)。请忽略丑陋的调试标签和可能不太好的格式(抱歉)。我想这有点像LazyList,但以下是详细说明,以防有人感到困惑。
第一步:设置缓存。
private LruCache<String, Bitmap> memoryCache;
private HashMap<String, String> idPairs = new HashMap<String, String>(); 

在我的方法中,我使用一个 LruCache 和一个 HashMap 来跟踪我已经下载的用户图片。稍后您将看到它是如何实现的,但其思想是避免不必要地从服务器下载内容。然后在您的 onCreate() 或其他相关方法中,初始化您的缓存。

    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    final int cacheSize = maxMemory / 8;
    memoryCache = new LruCache<String, Bitmap>(cacheSize)  {
        @Override 
        protected int sizeOf(String key, Bitmap bitmap){
            return (bitmap.getRowBytes() * bitmap.getHeight()) / 1024; //don't use getByteCount for API < 12
        }
    };

我的下一步是将默认的“空图像”位图添加到缓存中,以防止我到达没有与之关联的图片的条目。这样我只需要处理并添加此位图一次。

Bitmap defaultPicture = BitmapFactory.decodeResource(getResources(), R.drawable.default_user_picture);
    addBitmapToMemoryCache("default", defaultPicture);

现在是时候获取您需要的列表数据了!

步骤2:获取数据

private class GetPeopleData extends AsyncTask<JSONArray, Void, Void> {

    @Override
    protected Void doInBackground(JSONArray...lists) {
        HttpClient httpClient = new DefaultHttpClient(); 
        HttpPost httpPost = new HttpPost();
        ResponseHandler <String> responseHandler = new BasicResponseHandler();
        if(DEBUG) Log.d("GET PEOPLE DATA TASK", lists[0].length() + " users");
        getUsers(lists[0], httpClient, httpPost, responseHandler);
        return null;
    }

    @Override
    protected void onPostExecute(Void result){
        updateUserListView();
    }

}

这里有一个比较乏味的AsyncTask,它以JSONArray用户数据作为参数。我省略了部分内容,因为它基本上只是一个不需要解释的基本HTTP下载。getUsers方法是我开始准备添加到我的ListView中的内容。接下来我要做的就是处理从服务器下载的JSON,创建用户对象并将其添加到将显示的用户列表中。

private void getUsers(JSONArray userArray, HttpClient httpClient, HttpPost httpPost, ResponseHandler<String> responseHandler){
    if(DEBUG) Log.d("USERS ARRAY", userArray.length() + " users");
    try{

        users = new ArrayList<User>();
        if(DEBUG) Log.d("User Array -START", "" + users.size());
        //Go through userArray and get information needed for list
        for(int i = 0; i < userArray.length(); i++){
            User u = new User();
            if(DEBUG) Log.d("User Array - ADD USER", "" + users.size());
            String profileId = userArray.getJSONObject(i).getString("profileid");
            u.setId(userArray.getJSONObject(i).getString("id"));
            u.setDisplayName(userArray.getJSONObject(i).getString("displayname"));
            u.setStatus(userArray.getJSONObject(i).getString("status"));

            //check HashMap for sender/profileid pair
            if(idPairs.containsKey(profileId)){
                if(DEBUG) Log.d("idPairs", "User in HashMap");
                profileId = idPairs.get(profileId);
            } else {
                if(DEBUG) Log.d("idPairs", "User not in HashMap. Add profileId");
                idPairs.put("profileId", profileId);
            }
            u.setProfilePicture(getProfilePictureFromCache(profileId, httpClient, httpPost, responseHandler)); //check cache for image
            users.add(u);
            if(DEBUG) Log.d("User info", u.toString());
        }
    } catch (Exception e) {
        Log.e("BACKGROUND_PROC", e.getMessage());
    }
}

我将这张图片与最终要显示的用户对象相关联。我认为这不是完全浪费时间,因为图像是从位图缓存中获取的。
private Bitmap getProfilePictureFromCache(String profileId, HttpClient httpClient, HttpPost httpPost, ResponseHandler<String> responseHandler){
    Bitmap defaultPicture = getBitmapFromMemCache("default");
    Bitmap profilePicture = getBitmapFromMemCache(profileId);
    if(profilePicture != null){
        return profilePicture;
    } else {        
        String pictureString = getProfilePic(profileId, httpClient, httpPost, responseHandler);
        if(!pictureString.isEmpty()){       
            byte[] decodedString = Base64.decode(pictureString, Base64.DEFAULT);
            profilePicture = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
            addBitmapToMemoryCache(profileId, profilePicture);
            if(DEBUG) Log.d("MEMCACHE", "Download and store picture for " + profileId);
            return profilePicture;
        } else if (defaultPicture != null && pictureString.equals(null)) {
            if(DEBUG) Log.d("MEMCACHE", "Load default picture");
            return defaultPicture;
        }
    }
    return defaultPicture;
}

如果缓存中存在图片,则返回该图片;否则将下载。对于展示用户的应用,每个图片都会根据用户ID进行缓存,因此即使有多次出现在列表中的用户,也只有一个图片存储在其缓存中。

ImageView profile_picture = (ImageView) v.findViewById(R.id.profile_picture);
if(profile_picture != null){
                profile_picture.setImageBitmap(u.getProfilePicture());
            }

您需要在您的适配器中找到ImageView,并将该视图设置为与列表中对象相关联的图片即可。

1

您想要一个易于使用的东西吗?它可以处理缓存文件,删除缓存,并在内存接近边界线时释放吗?

试试LazyList,看看它:

https://github.com/nicolasjafelle/LazyList

正如所解释的那样,您只需要在您的应用程序类或任何其他活动或片段中创建单例:
//To use the default external folder, this folder will be deleted when the app no longe exists.
ImageLoader.getInstance().init(getApplicationContext());
//Or to use another folder, use...
//ImageLoader.getInstance().init(getApplicationContext(), "MyExternalFolder");

然后在显示图像的 ImageView 中,您需要实现 ImageProcessingCallback 接口:

public class MyItemView extends LinearLayout implements ImageProcessingCallback {
...
//Do what you need in methods onImagePreProcessing() and onImageProcessing(Bitmap     bitmap)
...
ImageLoader.getInstance().displayImage(data[position], myView);

最后,您需要在androidManifest.xml中添加此权限。
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

就是这样。然后你就有了清除 RAM 缓存或磁盘缓存或两者都清除的方法。


0

我不知道如何存储图片,因为我的应用程序使用来自APK的图片。然而,我认为如果要显示这些图片,您需要自定义ArrayAdapter。

这会为每个项目填充一个布局并将其放置在列表中。此布局由您创建并在其构造中指定的XML布局定义。您应该尝试使用自定义适配器扩展当前用于列表视图的任何适配器,该适配器根据项设置图像视图。

我使用ImageView.setImageResource(资源ID)来完成这个过程。但是,您的结果可能会有所不同。我将我的图片放在APK中,不确定如何从外部源显示它们。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接