gpt4 book ai didi

android - 在没有联系人图像可用时显示图像

转载 作者:太空狗 更新时间:2023-10-29 16:42:47 26 4
gpt4 key购买 nike

我编写了一个应用程序,可以简单地显示手机上所有可用的联系人。该应用程序运行良好。它可以检索联系人图像和显示名称。但是,在滚动列表时,应用程序有些滞后。我想知道如何消除这种滞后。同时,我想在没有联系人图像时显示图像。我试图制作一个 bool 标志,告诉我接触图像光标是否为空,如果是,那么我将标志 (image_found) 设置为 false,然后尝试使用 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;
}
}

}

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

[重新编辑]这次。图片全乱了。

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

}

最佳答案

除了 Gabe Sechan 所说的 View 每次都被夸大之外,您还应该更改一些内容。不要使用内容提供者获取照片,而是使用 Contacts 类中的 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 作为缓存键。在检查缓存之前的某处适配器中,我有以下行:字符串键 = uri.toString();

关于android - 在没有联系人图像可用时显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15012619/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com