你可以使用universalimageloader来做到这一点https://github.com/nostra13/Android-Universal-Image-Loader
正如您在下面的示例中看到的那样,您可以将联系人照片保存到 SD 购物车,然后将他们的 FilePath 放入列表中,并将该路径与通用图像加载器一起使用,
您可以从https://dl.dropbox.com/u/68130108/UniversalImageLoaderExample.rar获取完整示例
在你的适配器 getView
imageLoader.displayImage(contactFilePath, holder.image, displayImageOptions);
在您的活动公共类 ImageListActivity 扩展 BaseActivity { AdapterContact adapterContact; DisplayImageOptions 选项;字符串[] imageUrls; ArrayList 联系人列表 = 新的 ArrayList(); 列表视图列表视图;ProgressDialog 对话框;@Override 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";
}
}