3

我必须在具有自定义光标适配器实现的列表视图中显示我的联系人的缩略图。首先,我使用 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 中一切正常。请帮忙。谢谢!

4

0 回答 0