gpt4 book ai didi

安卓 :how to get the contactlist images into adapter

转载 作者:行者123 更新时间:2023-11-30 03:52:28 27 4
gpt4 key购买 nike

嗨,我正在尝试将手机中的联系人图像检索到我的阵列适配器中

  @Override
public View getView(int position, View convertView, ViewGroup parent) {

View view = convertView;

if (view == null) {
LayoutInflater inflater = (LayoutInflater) (getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
view = inflater.inflate(renderer, null);
}


TextView text = (TextView) view.findViewById(R.id.name);
ImageView photo = (ImageView) view.findViewById(R.id.photo);
new LoadImage(photo).execute();

我想使用 asynctask 从手机中获取照片,我对如何在 doinbackground() 中做到这一点感到震惊。请建议如何从联系人中获取照片,doinbackground() 过程中发生了什么

class LoadImage extends AsyncTask<Object, Void, Bitmap>{

private ImageView imv;


public LoadImage(ImageView imv) {
this.imv = imv;

}



@Override
protected Bitmap doInBackground(Object... params) {
Bitmap bitmap = null;
photo.setImageBitmap(contact.getPhoto());
}
@Override
protected void onPostExecute(Bitmap result) {

}
}

最佳答案

您可以使用 universalimageloader 来执行此操作 https://github.com/nostra13/Android-Universal-Image-Loader

正如您在下面的示例中看到的,您可以将联系人照片保存到 SD 购物车,然后将其文件路径放入列表中,并将该路径与通用图像加载器一起使用,

您可以从 https://dl.dropbox.com/u/68130108/UniversalImageLoaderExample.rar 中获取完整示例

在你的适配器 getView 中

imageLoader.displayImage(contactFilePath, holder.image, displayImageOptions);

在你的 Activity 中 公共(public)类 ImageListActivity 扩展 BaseActivity { AdapterContact adapterContact; DisplayImageOptions 选项; 字符串[] imageUrls; ArrayList contactList = new ArrayList(); ListView ListView ; ProgressDialog对话框; @覆盖 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ac_image_list);

        listView = (ListView) findViewById(android.R.id.list);

new asynGetContacts().execute();

}

protected class asynGetContacts extends AsyncTask<String, Void, Integer> {
protected Integer doInBackground(String... params) {
try {
ImageListActivity.this.runOnUiThread(new Runnable() {
public void run() {
dialog = ProgressDialog.show(ImageListActivity.this, "","Lütfen bekleyin...", true);
dialog.show();
}
});
Uri uri = ContactsContract.Contacts.CONTENT_URI;
ContentResolver cr = getContentResolver();
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
Cursor cur = cr.query(uri, null, null, null, sortOrder);
if (cur.getCount() > 0) {
String id;
String name;
while (cur.moveToNext()) {
Contact c = new Contact();
id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Uri my_contact_Uri = Uri.withAppendedPath(
ContactsContract.Contacts.CONTENT_URI,
String.valueOf(id));

InputStream inputStream = ContactsContract.Contacts
.openContactPhotoInputStream(getContentResolver(),
my_contact_Uri);
BufferedInputStream buf = new BufferedInputStream(inputStream);
Bitmap my_btmp = BitmapFactory.decodeStream(buf);

if (my_btmp != null) {
c.photo = my_btmp;
c.id = id;
c.name = name;
c.photoURL = saveToSD(my_btmp, id);
contactList.add(c);
}
}
}
cur.close();



} catch (Exception e) {
Log.v("hata", e.toString());
return 0;
}

return 1;
}

protected void onPostExecute(Integer result) {
try {
dialog.dismiss();
adapterContact = new AdapterContact(ImageListActivity.this, 0,
contactList);
listView.setAdapter(adapterContact);
listView.setOnScrollListener(new PauseOnScrollListener(false, true));
} catch (Exception e) {
Log.v("hata", e.toString());
}

super.onPostExecute(result);
}
}

public String saveToSD(Bitmap bmp, String id) {
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ContactImage/";

try {
File dir = new File(file_path);

if (!dir.exists())
dir.mkdirs();

File f = getFileStreamPath(file_path);

if (!f.exists()) {
File file = new File(dir, "contact_" + id + ".png");
FileOutputStream fOut = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
}


} catch (Exception e) {
Log.v("Hata", e.toString());
}

return "file://"+file_path+"contact_" + id + ".png";
}

}

关于安卓 :how to get the contactlist images into adapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13928836/

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