2

我编写了一个应用程序,它只是向我显示手机上所有可用的联系人。该应用程序运行良好。它可以检索联系人图像和显示名称。但是,在滚动列表时,应用程序有些滞后。我想知道如何消除这种滞后。同时,我想在没有可用的联系人图像时显示图像。我试图创建一个布尔标志,告诉我联系人图像光标是否为空,如果是,那么我将标志 (image_found) 设置为 false,然后尝试使用 setImageResource 设置图像,但它不起作用。好吧,然后我认为当没有图像时,blob 对象可能会得到一个空值,但它也很有效。这是我的 mail_activity 代码。

package legacy_systems.aggregatedcontactlist;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ListActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Cursor c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, 
                                              null, 
                                              null, 
                                              null, 
                                              ContactsContract.Contacts.DISPLAY_NAME+" ASC");

        ArrayList<String> con_ids = new ArrayList<String>();
        if(c!=null)
        {
            for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
                con_ids.add(c.getString(c.getColumnIndex(ContactsContract.Contacts._ID)));
            }
        }



        ListView ls = getListView();

        MyAdapter ada = new MyAdapter(this, R.layout.listview, con_ids);

        ls.setAdapter(ada);
    }

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


}

这是 MyAdapter 的代码

package legacy_systems.aggregatedcontactlist;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentUris;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class MyAdapter extends ArrayAdapter<String>{
    Context cont;
    boolean image_found = true;
    ArrayList<String> ids;

    public MyAdapter(Context context, int resource,
            ArrayList<String> objects) {
        super(context, resource, objects);
        cont = context;
        ids = objects;
        // TODO Auto-generated constructor stub
    }

    public View getView(int position, View ConvertView, ViewGroup parent){
        //For Contact Photo
        Bitmap photo=null;
        //Inflator Work
        LayoutInflater infl = (LayoutInflater)cont.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = infl.inflate(R.layout.list_view, parent, false);

        //Retreiving photo and Setting Photo
        photo = BitmapFactory.decodeStream(retrievePhoto(ids.get(position)));
        ImageView iv = (ImageView)row.findViewById(R.id.listicon);
        iv.setImageBitmap(photo);

        if(!image_found)
        {
            iv.setImageResource(R.drawable.person);
        }

        //Retrieving ContactName
        TextView tv  =(TextView)row.findViewById(R.id.listtext);
        tv.setText(retrieveName(ids.get(position)));

        return row;
    }

    private InputStream retrievePhoto(String Id)
    {
        Uri ContactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, Long.valueOf(Id));
        Uri PhotoUri = Uri.withAppendedPath(ContactUri, Contacts.Photo.CONTENT_DIRECTORY);

        Cursor c = cont.getContentResolver().query(PhotoUri, new String[]{Contacts.Photo.PHOTO}, null,null,null);

        try
        {
            if(c.moveToFirst())
            {
                byte[] data = c.getBlob(0);
                if(data!=null)
                {
                    return new ByteArrayInputStream(data);
                }
                else
                {
                    image_found = false;
                }
            }
        }
        finally
        {
            c.close();
        }
        return null;
    }

    private String retrieveName(String Id)
    {
        Uri ContactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, Long.valueOf(Id));
        Cursor c = cont.getContentResolver()
                        .query(ContactUri, 
                                new String[]{ContactsContract.Contacts.DISPLAY_NAME}, 
                                null, 
                                null, 
                                null);
        if(c==null)
            return null;
        try
        {
            if(c.moveToFirst())
            {
                String name = c.getString(0);
                return name;
            }
        }
        finally
        {
            c.close();
        }
        return null;
    }

}

我真的很高兴我能走到这一步。请帮助我继续我的冒险。

[编辑] 我已经实施了建议的事情,现在我无法检索单个联系人图像。我不知道问题是什么。我只在 MyAdapter.java 中进行了更改,并且我正在发布它的代码。

package legacy_systems.aggregatedcontactlist;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentUris;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class MyAdapter extends ArrayAdapter<String>{
    Context cont;
    boolean image_found = true;
    ArrayList<String> ids;

    public MyAdapter(Context context, int resource,
            ArrayList<String> objects) {
        super(context, resource, objects);
        cont = context;
        ids = objects;
        // TODO Auto-generated constructor stub
    }

    public View getView(int position, View ConvertView, ViewGroup parent){

        View row = ConvertView;
        //For Contact Photo
        Bitmap photo=null;
        //Inflator Work
        if(row==null)
        {
            LayoutInflater infl = (LayoutInflater)cont.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = infl.inflate(R.layout.list_view, parent, false);
        }

        //Retreiving photo and Setting Photo
        ImageView iv = (ImageView)row.findViewById(R.id.listicon);


        synchronized (iv) {
            Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, Long.valueOf(ids.get(position)));
            PhotoLoader loader = new PhotoLoader(iv, contactUri);
            loader.execute();

        }



        //Retrieving ContactName
        TextView tv  =(TextView)row.findViewById(R.id.listtext);
        tv.setText(retrieveName(ids.get(position)));

        return row;
    }



    private String retrieveName(String Id)
    {
        Uri ContactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, Long.valueOf(Id));
        Cursor c = cont.getContentResolver()
                        .query(ContactUri, 
                                new String[]{ContactsContract.Contacts.DISPLAY_NAME}, 
                                null, 
                                null, 
                                null);
        if(c==null)
            return null;
        try
        {
            if(c.moveToFirst())
            {
                String name = c.getString(0);
                return name;
            }
        }
        finally
        {
            c.close();
        }
        return null;
    }

    class PhotoLoader extends AsyncTask<Void, Void, Bitmap>
    {
        final WeakReference<ImageView> mView;
        final Uri mUri;

        public PhotoLoader(ImageView view, Uri uri) {

            if(view == null)
            {
                throw new IllegalArgumentException("View Cannot be null");
            }
            if(uri == null)
            {
                throw new IllegalArgumentException("Uri cant be null");
            }
            mView = new WeakReference<ImageView>(view);
            mUri = uri;
        }

        protected Bitmap doInBackground(Void...args){
            Bitmap bitmap;
            InputStream in = ContactsContract.Contacts.openContactPhotoInputStream(cont.getContentResolver(), mUri);
            bitmap = BitmapFactory.decodeStream(in);

            if(bitmap == null)
            {   Resources mResources = cont.getResources();
                bitmap = BitmapFactory.decodeResource(mResources, R.drawable.person);
            }
            return bitmap;
        }
    }

}

我还没有实现缓存。当这个问题解决后,我会尝试实现缓存和Holder。在此之前,请解决问题,

[重新编辑] 这次。图像都乱了。

package legacy_systems.aggregatedcontactlist;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentUris;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class MyAdapter extends ArrayAdapter<String>{
    Context cont;
    boolean image_found = true;
    ArrayList<String> ids;
    ImageView iv;

    public MyAdapter(Context context, int resource,
            ArrayList<String> objects) {
        super(context, resource, objects);
        cont = context;
        ids = objects;
        // TODO Auto-generated constructor stub
    }

    public View getView(int position, View ConvertView, ViewGroup parent){

        View row = ConvertView;
        //For Contact Photo

        //Inflator Work

        if(row==null)
        {
            LayoutInflater infl = (LayoutInflater)cont.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = infl.inflate(R.layout.list_view, parent, false);
        }


        //Retreiving photo and Setting Photo
        iv = (ImageView)row.findViewById(R.id.listicon);


        synchronized (this) {
            Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, Long.valueOf(ids.get(position)));
            PhotoLoader loader = new PhotoLoader(iv, contactUri);
            loader.execute();

        }



        //Retrieving ContactName
        TextView tv  =(TextView)row.findViewById(R.id.listtext);
        tv.setText(retrieveName(ids.get(position)));

        return row;
    }



    private String retrieveName(String Id)
    {
        Uri ContactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, Long.valueOf(Id));
        Cursor c = cont.getContentResolver()
                        .query(ContactUri, 
                                new String[]{ContactsContract.Contacts.DISPLAY_NAME}, 
                                null, 
                                null, 
                                null);
        if(c==null)
            return null;
        try
        {
            if(c.moveToFirst())
            {
                String name = c.getString(0);
                return name;
            }
        }
        finally
        {
            c.close();
        }
        return null;
    }

    class PhotoLoader extends AsyncTask<Void, Void, Bitmap>
    {
        final WeakReference<ImageView> mView;
        final Uri mUri;

        public PhotoLoader(ImageView view, Uri uri) {

            if(view == null)
            {
                throw new IllegalArgumentException("View Cannot be null");
            }
            if(uri == null)
            {
                throw new IllegalArgumentException("Uri cant be null");
            }
            mView = new WeakReference<ImageView>(view);
            mUri = uri;
        }

        protected Bitmap doInBackground(Void...args){
            Bitmap bitmap;
            InputStream in = ContactsContract.Contacts.openContactPhotoInputStream(cont.getContentResolver(), mUri);
            bitmap = BitmapFactory.decodeStream(in);


            return bitmap;
        }

        protected void onPostExecute(Bitmap bitmap)
        {
            if(bitmap == null)
            {   Resources mResources = cont.getResources();
                bitmap = BitmapFactory.decodeResource(mResources, R.drawable.person);
                iv.setImageBitmap(bitmap);
                return;
            }
            iv.setImageBitmap(bitmap);
        }
    }

}
4

3 回答 3

2

我终于解决了我所有的问题。我突然想到,应用程序最好不要使用自定义适配器,因此,使用已经提供的适配器总是好的。我的情况是,我决定使用 SimpleCursorAdapter。为了解决在没有联系人图像时显示图像的问题,一个简单的需要定义一个预定义的图像作为布局文件中联系人图像位置的来源。如果联系人没有图像,则位图包含 null,因此,当联系人图像不存在时,技巧是不要更改图像。

这是我的 MyAdapter 代码,它简单地扩展了 SimpleCursorAdapter。

package com.example.contact;

import java.io.InputStream;
import java.util.ArrayList;



import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.support.v4.util.LruCache;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.widget.ImageView;

public class MyAdapter extends SimpleCursorAdapter{

    private Cursor cursor;  
    private static int con_id_index;
    private static Drawable person;
    private Context context;
    private static LruCache<Long, Bitmap> cache;
    long con_id;
    private ArrayList<Long> id_list = new ArrayList<Long>();

    public MyAdapter(Context context, int layout, Cursor c, String[] from,
            int[] to, int flags) {
        super(context, layout, c, from, to, flags);
        this.cursor = c;        
        con_id_index = cursor.getColumnIndex(ContactsContract.Contacts._ID);
        person = context.getResources().getDrawable(R.drawable.person);
        this.context = context;

        final int maxMem = (int)Runtime.getRuntime().maxMemory()/1024;
        final int cacheSize = maxMem/8;

        cache = new LruCache<Long, Bitmap>(cacheSize);
        id_list.clear();
    }   


    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        Object tag;
        // TODO Auto-generated method stub
        super.bindView(view, context, cursor);

        con_id = cursor.getLong(con_id_index);
        if(!id_list.contains(Long.valueOf(con_id)))
        {
            id_list.add(con_id);
        }
        Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, con_id);

        ImageView con_image = (ImageView) view.findViewById(R.id.contact_iamge);
        tag = con_id;
        con_image.setImageDrawable(person);
        con_image.setTag(tag);
        if(getBitmapFromCache(con_id)!=null)
        {
            con_image.setImageBitmap(getBitmapFromCache(con_id));
        }
        else
        {
            ImageLoader loader = new ImageLoader(con_image, contactUri, con_id);
            loader.execute();
        }

    }

    class ImageLoader extends AsyncTask<Void, Void, Bitmap>
    {
        private ImageView mView;
        private Uri mUri;       
        private Object tag;
        private long position;

        public ImageLoader(ImageView view, Uri uri, long position) {

            if(view == null)
            {
                throw new IllegalArgumentException("View Cannot be null");
            }
            if(uri == null)
            {
                throw new IllegalArgumentException("Uri cant be null");
            }
            mView = view;
            tag = mView.getTag();
            this.position = position;
            mUri = uri;         
        }

        protected Bitmap doInBackground(Void...args){
            Bitmap bitmap;

            //Load image from the Content Provider
            InputStream in = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), mUri);
            bitmap = BitmapFactory.decodeStream(in);

            return bitmap;
        }

        protected void onPostExecute(Bitmap bitmap)
        {
            //If is in somewhere else, do not temper
            if(!mView.getTag().equals(tag))
                return;
            //If no image was there and do not put it to cache
            if(bitmap!= null)
            {   

                mView.setImageBitmap(bitmap);
                addBitmapToCache(position, bitmap);
                return;
            }
            //Otherwise, welcome to cache           
            return;
        }
    }
    /**Add image to cache*/
    private void addBitmapToCache(Long key, Bitmap bitmap)
    {
        if(getBitmapFromCache(key)==null)
        {
            cache.put(key, bitmap);
        }
    }
    /**Retrive image from cache*/
    private Bitmap getBitmapFromCache(Long key)
    {
        return cache.get(key);
    }

    public ArrayList<Long> getIDList()
    {
        return id_list;
    }

}

因为,加载的游标不包含图像,并且内容提供程序不允许我们一次查询多个表。因此,最好在后台线程中检索图像。那就是我使用 ASyncTask 的地方。

MyAdpter 不会覆盖 getView(),而是覆盖 bindView(),它允许向正在膨胀的视图添加/绑定附加信息。适配器利用内存缓存,并使用联系人 ID 作为键来检索和存储缓存中的图像。

希望它可以帮助别人。

于 2013-03-29T19:39:19.453 回答
2

除了 Gabe Sechan 所说的每次都夸大了视图之外,你应该改变一些事情。不要使用内容提供者来获取照片,而是使用openContachPhotoInputStreamContacts 类中的方法,您可以在此处找到该方法。您还应该使用 AsyncTask 将位图的打开和加载移动到后台线程。最后,尝试使用 aLruCache来缓存已经打开的位图以提高效率,如此处所述。我最近不得不做一些类似的事情,所以我分享了一段代码来帮助你。我用它来加载背景上的图像。

class PhotoLoader extends AsyncTask<Void, Void, Bitmap> {

    final WeakReference<ImageView> mView;
    final Uri mUri;

    PhotoLoader(ImageView view, Uri uri) {
        if (view == null) {
            throw new IllegalArgumentException("View cannot be null!");
        }

        if (uri == null) {
            throw new IllegalArgumentException("Uri cannot be null!");
        }

        mView = new WeakReference<ImageView>(view);
        mUri = uri;
    }

    @Override
    protected Bitmap doInBackground(Void... args) {
                    // use the uri passed to the constructor
        InputStream is = Contacts.openContactPhotoInputStream(mResolver,
                mUri);
        Bitmap bm = BitmapFactory.decodeStream(is);

                    // load custom resource image if contact has no photo
        if (bm == null) {
            bm = BitmapFactory.decodeResource(mResources,
                    R.drawable.ic_contact);
        }

        return bm;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
                    // use a transition drawable for nice fade effect
        Drawable[] layers = new Drawable[2];
        layers[0] = mResources.getDrawable(android.R.color.transparent);

        layers[1] = new BitmapDrawable(mResources, result);
        TransitionDrawable t = new TransitionDrawable(layers);
        t.setCrossFadeEnabled(true);

        ImageView view = mView.get();

        if(view != null) {
            view.setImageDrawable(t);
            t.startTransition(mShortAnimation);
        }
        synchronized (mPhotoCache) {
                            // store bitmap in LruCache for later use...
            mPhotoCache.put(mUri.toString(), result);
        }
    }
}

我在适配器中这样使用它:

synchronized (mPhotoCache) {
    Bitmap b = mPhotoCache.get(key);

    if (b != null) {
    imageView.setImageBitmap(b);
    } else {
    imageView.setImageResource(android.R.color.transparent);
    PhotoLoader loader = new PhotoLoader(imageView, uri);
    loader.execute();
    }
}

编辑:为了澄清,我使用字符串格式的联系人 uri 作为缓存键。在检查缓存之前的某个适配器中,我有以下行: String key = uri.toString();

于 2013-02-21T22:08:52.657 回答
1

您的 getView 每次通话都会增加一个新行。它应该只在传入的视图为空时才这样做,否则它应该覆盖传入的视图。这将加快很多滞后

于 2013-02-21T21:36:33.673 回答