Android 在bindView(光标适配器)中设置图像如何区分具有图像数据的Uri和不具有图像数据的Uri

Android 在bindView(光标适配器)中设置图像如何区分具有图像数据的Uri和不具有图像数据的Uri,android,Android,我试图在游标适配器的bindView方法中设置ListView中的图像,实际上,所有数据(与联系人相关)都预取到数据库表中。我查询此表以获取我的图像uri(对于每个联系人,无论是否有图像,都有一个图像uri)。现在对于那些没有图像的联系人,我想显示一个默认图像。然而,我尝试了以下代码,但是我的图像在Uri位置没有图像的视图中被Uri位置有数据的图像(其他联系人图像)重复 以下是我的代码: @Override public void bindView(View vi

我试图在游标适配器的bindView方法中设置ListView中的图像,实际上,所有数据(与联系人相关)都预取到数据库表中。我查询此表以获取我的图像uri(对于每个联系人,无论是否有图像,都有一个图像uri)。现在对于那些没有图像的联系人,我想显示一个默认图像。然而,我尝试了以下代码,但是我的图像在Uri位置没有图像的视图中被Uri位置有数据的图像(其他联系人图像)重复

以下是我的代码:

        @Override
        public void bindView(View view, Context context, Cursor cursor) {

            ((TextView) view.getTag(R.id.textView1)).setText(cursor.getString(cursor.getColumnIndexOrThrow("Name")));
            ((TextView) view.getTag(R.id.textView2)).setText(cursor.getString(cursor.getColumnIndexOrThrow("BirthDate")));
            ((TextView) view.getTag(R.id.textView1)).setTypeface(tf);
            ((TextView) view.getTag(R.id.textView2)).setTypeface(tf); 

            String image = cursor.getString(cursor.getColumnIndexOrThrow("imageUri"));
            Uri IMAGE_URI = Uri.parse(image); 

            InputStream stream = Contacts.openContactPhotoInputStream(context.getContentResolver(), IMAGE_URI);
            if (stream == null) {
                ((ImageView) view.getTag(R.id.imageView1)).setBackgroundResource(R.drawable.arrow_01); 
            }
            if(stream != null){
                BufferedInputStream buf = new BufferedInputStream(stream);

                Bitmap my_btmp = BitmapFactory.decodeStream(buf);
                ((ImageView) view.getTag(R.id.imageView1)).setImageBitmap(my_btmp);
            }



        }

有什么想法吗

试试这个,它会解决你的问题

   @Override
   public void bindView(View view, Context context, Cursor cursor) {

       ((TextView) view.getTag(R.id.textView1)).setText(cursor.getString(cursor.getColumnIndexOrThrow("Name")));
       ((TextView) view.getTag(R.id.textView2)).setText(cursor.getString(cursor.getColumnIndexOrThrow("BirthDate")));
       ((TextView) view.getTag(R.id.textView1)).setTypeface(tf);
       ((TextView) view.getTag(R.id.textView2)).setTypeface(tf); 

       String image = cursor.getString(cursor.getColumnIndexOrThrow("imageUri"));
       Uri IMAGE_URI= Uri.withAppendedPath(image, Contacts.Photo.CONTENT_DIRECTORY);
       Bitmap map = getBitMapfromUri(context,IMAGE_URI.toString(),128,128); // Put your desirable height, whatever you want

       if(map == null){
           // Load your default Image
       }else{
           ((ImageView) view.getTag(R.id.imageView1)).setImageBitmap(map);
       }

   }

    public  Bitmap getBitMapfromUri(Context context, String uri,
            int width, int height) {

        Bitmap map = null;
        InputStream is = null;
        try {
             is = context.getContentResolver().openInputStream(
                    Uri.parse(uri));
            Options opts = new BitmapFactory.Options();
            opts.inSampleSize = 1;
            opts.inPurgeable = true;
            opts.inInputShareable = true;

            map = Bitmap.createScaledBitmap(
                    BitmapFactory.decodeStream(is, null, opts), width, height,
                    false);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                if(is!=null)
                    is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return map;
    }
已解决:

谢谢你给我一个提示:

这句话很重要:

IMAGE_URI= Uri.withAppendedPath(IMAGE_URI, Contacts.Photo.CONTENT_DIRECTORY);
最后在bindView中:

((ImageView) view.getTag(R.id.imageView1)).setImageURI(IMAGE_URI);
            if (((ImageView) view.getTag(R.id.imageView1)).getBackground()== null){
                ((ImageView) view.getTag(R.id.imageView1)).setBackgroundResource(R.drawable.arrow_01); 
        }

另一种克服这种情况的方法是直接查询联系人,但是我不确定它会对内存产生什么影响,以及列表中每一行的处理速度有多慢。好主意,但它总是返回空位图。在每个imageview上强制默认图像。是吗,你能检查一下缩略图的uri是否正确吗,我使用相同的代码,它对我有效,将一个示例uri粘贴到此处是的,如果我在没有位图转换的情况下尝试,我至少有一个联系人中有一个图像,我可以使用常规代码显示它。content://com.android.contacts/contacts/675 是我手机上的有效图像Uri,可转换为有效位图。我还尝试在if(is!=null)部分发送消息,但从未打印过。