カスタムカーソルアダプター実装を持つリストビューに、連絡先の連絡先のサムネイルを表示する必要があります。最初に asynctask を使用して実行し、doInBackground メソッドで、次のスニペットを使用してサムネイルのビットマップを取得しました。
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,
Long.parseLong(params[0]));
InputStream input = ContactsContract.Contacts
.openContactPhotoInputStream(
mContext.getContentResolver(), uri);
if (input != null) {
mThumb = BitmapFactory.decodeStream(input);
} else {
mThumb = defaultPhoto;
}
それは私に正しいビットマップを与えます。
その後、 Universal Image Loaderについて知りました。次のスニペットのように、カスタム BaseImageDownlaoder を実装しました。`パブリック クラス ContactImageDownloader は BaseImageDownloader を拡張します{
private static final String SCHEME_CONTACT_IMAGE = "content";
private static final String DB_URI_PREFIX = SCHEME_CONTACT_IMAGE + "://";
private Context mContext;
public ContactImageDownloader(Context context) {
super(context);
mContext = context;
}
@Override
protected InputStream getStreamFromOtherSource(String imageUri, Object extra) throws IOException {
if (imageUri.startsWith(DB_URI_PREFIX)) {
InputStream input = ContactsContract.Contacts
.openContactPhotoInputStream(
mContext.getContentResolver(), Uri.parse(imageUri));
ByteArrayOutputStream output = new ByteArrayOutputStream();
if (input!=null) {
IoUtils.copyStream(input, output);
} else {
//default image
Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.wiresimage);
bitmap.compress(CompressFormat.JPEG, BUFFER_SIZE, output);
}
byte[] imageData = output.toByteArray();
input.close();
return new ByteArrayInputStream(imageData);
} else {
return super.getStreamFromOtherSource(imageUri, extra);
}
}`
}
しかし、それは画像を取得しません。ログには、java.io.FileNotFoundException: File does not exist; URI: content://com.android.contacts/contacts/567, calling user: com.c
次のように表示されます。したがって、実装を検証する (カスタム BaseImageDownloader が間違っているかどうかを確認する) ために、asynctask の doInBackground メソッドで使用したスニペットをカーソル アダプターの bindView メソッドに配置しました。それでも、InputStream はヌル、つまり FileNotFoundException です。しかし、すべてが Asynctask で正しく機能します。助けてください。ありがとう!