异步将图像从Internet /数据库下载到Android ListView

时间:2012-03-15 10:58:56

标签: android

我正在尝试在列表中设置drawable。我的列表不包含图像。 我正在使用带有几个TextView和一个ImageView的list_item.xml填充它。

如果我的图像存储在Res android文件夹中,我没有任何问题,但是我在这里从数据库加载此图像。

现在我正在使用这种方法之王:

SimpleAdapter adapter = new SimpleAdapter(this.getBaseContext(), list,
R.layout.xml_leaderboard_item, keys, views);

有没有办法在我的列表中添加这个drawable / bitmap?或者可能以编程方式将该图像放在Res文件夹中以便我可以使用它?

2 个答案:

答案 0 :(得分:1)

使用BitmapFactory类解码数据库中的图像字节数据。然后在ImageView上使用setImageBitmapsetImageDrawable方法。

byte[] imageData = ...;    
Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
imageView.setImageBitmap( bitmap );

答案 1 :(得分:0)

我终于能够做我想做的事了。结果如下:

enter image description here

我正在从我的数据库中异步下载这些图片。我无法将其添加到ListView中... 这是我可能感兴趣的代码。

public class LeaderboardScreen extends ListActivity implements View.OnClickListener
{
    private UserAdapter             adapter;
    private ArrayList<User>         users;
    private UserPictureDownloader   user_picture_cache;
    ...

    protected void onCreate(Bundle saved_instance_state)
    {
        ...
        users = new ArrayList<User>();
        adapter = new UserAdapter(this, R.layout.xml_leaderboard_item, users);
        user_picture_cache = new UserPictureDownloader();
        setListAdapter(adapter);     
        ...
    }     


    private class UserAdapter extends ArrayAdapter<User>
    {
        private ArrayList<User>     users;

        public UserAdapter(Context context, int text_view_res_id, ArrayList<User> users)
        {
            super(context, text_view_res_id, users);
            this.users = users;
        }

        public View         getView(int position, View view, ViewGroup parent)
        {
            LayoutInflater layout = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layout.inflate(R.layout.xml_leaderboard_item, null);


            ImageView iv_pic = (ImageView)view.findViewById(R.id.lead_pic);
            TextView et_position = (TextView)view.findViewById(R.id.lead_position);
            TextView et_user = (TextView)view.findViewById(R.id.lead_user);
            TextView et_score = (TextView)view.findViewById(R.id.lead_s_value);
            TextView et_reput = (TextView)view.findViewById(R.id.lead_r_value);
            TextView et_contrib = (TextView)view.findViewById(R.id.lead_c_value);
            TextView et_bronze = (TextView)view.findViewById(R.id.lead_bronze_value);
            TextView et_silver = (TextView)view.findViewById(R.id.lead_silver_value);
            TextView et_gold = (TextView)view.findViewById(R.id.lead_gold_value);

            User current_user = users.get(position);
            if (current_user != null)
            {               
                // Set available informations right away.
                et_position.setText(String.valueOf(position + 1));
                et_user.setText(current_user.GetLogin());
            et_score.setText(String.valueOf(current_user.GetScore()));
            et_reput.setText(String.valueOf(current_user.GetReputation()));
            et_contrib.setText(String.valueOf(current_user.GetNumberOfQuestionPost()));
            et_bronze.setText(String.valueOf(current_user.GetBadgeBronze()));
            et_silver.setText(String.valueOf(current_user.GetBadgeSilver()));
            et_gold.setText(String.valueOf(current_user.GetBadgeGold()));

            // Fetch user image asynchronously.
            final int pos = position;
            final String user_login = current_user.GetLogin();
            final ImageView _iv_pic = iv_pic;

            final Handler handler = new Handler()
                {
                @Override
                public void         handleMessage(Message message)
                {
                    _iv_pic.setImageDrawable((Drawable)message.obj);
                }
            };

            Thread thread = new Thread()
            {
                @Override
                public void         run()
                {
                    try
                    {
                        Drawable dwb = (Drawable)user_picture_cache.GetCachedObject(user_login, handler);
                        handler.sendMessage(handler.obtainMessage(0, dwb));
                    }
                    catch (Exception e) {   Log.e(TAG, e.getMessage()); }
                }
            };
            thread.start();
        }
        return view;
    }
}   

DownloadCache类:

public class DownloadCache
{
    private final String    TAG = "DownloadCache";

    private ConcurrentHashMap<String, CountDownLatch>   queued_downloads;
    private ConcurrentHashMap<String, Object>           cache;

    public DownloadCache()
    {
        queued_downloads = new ConcurrentHashMap<String, CountDownLatch>();
        cache = new ConcurrentHashMap<String, Object>();
    }


    public Object GetCachedObject(String url, Handler handler) throws InterruptedException
    {
        // Check for a running download, if found wait for its result.
        CountDownLatch complete_event = queued_downloads.get(url);
        if (complete_event != null)
        complete_event.await();

        // Check for cached object.
        Object obj = cache.get(url);
        if (obj != null)
            return obj;

        obj = StartDownload(url, handler);
        return obj;
    }

private Object StartDownload(String url, Handler handler)
    {
        CountDownLatch complete_event = new CountDownLatch(1);
        queued_downloads.put(url, complete_event);

        Object obj = DoDownloadWork(url, handler);
        cache.put(url, obj);

        queued_downloads.remove(url);

        complete_event.countDown();
        return obj;
    }

    public Object DoDownloadWork(String url, Handler handler)
    {
        return null;
    }
}

希望得到这个帮助。