【问题标题】:Setting images in bindView (Cursor Adapter) how to differentiate between Uri that has image data and the the one that does not在 bindView (Cursor Adapter) 中设置图像如何区分具有图像数据的 Uri 和没有图像数据的 Uri
【发布时间】:2013-12-18 16:02:40
【问题描述】:

我试图在光标适配器的 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);
            }



        }

有什么想法吗?

【问题讨论】:

  • 解决这个问题的另一种方法是直接查询联系人,但我不确定它会对内存产生什么影响,以及列表中每一行的处理速度会有多慢。

标签: android


【解决方案1】:

试试这个,它应该可以解决你的问题。

   @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;
    }

【讨论】:

  • 好主意,但它总是返回一个空位图。在每个图像视图上强制使用默认图像。
  • 是吗,你能检查缩​​略图的 uri 是否正确,我使用相同的代码并且它对我有用,在这里粘贴一个示例 uri
  • 是的,如果我在没有位图转换的情况下尝试它,我至少在一个联系人中有一张图像,我可以使用常规代码显示它。
  • content://com.android.contacts/contacts/675 是我手机上的有效图像 Uri,它转换为有效的位图。
  • 我还尝试在 if(is!=null) 部分添加一条消息,但从未打印出来。
【解决方案2】:

已解决:

感谢Techfist给我提示:

这句话很重要:

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); 
        }

【讨论】:

    猜你喜欢
    • 2013-06-24
    • 2023-03-15
    • 2021-02-27
    • 2014-10-18
    • 1970-01-01
    • 2017-05-16
    • 1970-01-01
    • 2016-02-17
    • 2013-09-14
    相关资源
    最近更新 更多