我写了一个应用程序,它只是向我显示手机上所有可用的联系人。该应用程序运行正常。它可以检索联系人图像和display_name。但是,在滚动列表时,应用程序有些滞后。我想知道如何消除这种滞后。同时,我想在没有联系人图像的情况下显示图像。我试图制作一个布尔标志告诉我接触图像光标是否为空,如果是,那么我将标志(image_found)设置为假,然后尝试使用setImageResource设置图像,但它不起作用。那么,我认为当没有图像时,blob对象可能会得到一个null,但它也有效。这是我的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;
}
}
}
我还没有实现Cache。当这个问题得到解决时,我会尝试实现缓存和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);
}
}
}
答案 0 :(得分:2)
除了Gabe Sechan所说的关于每次充气的观点之外,你应该改变一些事情。使用“联系人”类中的openContachPhotoInputStream
方法,而不是使用内容提供商来获取照片,您可以找到here。您还应该使用AsyncTask将位图的打开和加载移动到后台线程。最后,尝试使用LruCache
缓存已打开的位图以提高效率,如here所述。我最近不得不做类似的事情所以我正在分享一段代码来帮助你。我用它来加载背景图像。
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();
答案 1 :(得分: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作为密钥来检索和存储缓存中的图像。
希望它可以帮助别人。
答案 2 :(得分:1)
你的getView每次通话都会给新行充气。它应该只在传入的视图为null时才这样做,否则它应该覆盖传入的视图。这将加速很多延迟