【问题标题】:How to display contact photo in widget Android 4+?如何在小部件 Android 4+ 中显示联系人照片?
【发布时间】:2013-01-23 12:49:03
【问题描述】:

我正在尝试使用 RemoteViews.setImageViewUri() 在主屏幕小部件上显示联系人图片,但没有显示任何内容。

我记录了图片 URI,它是正确的,例如 content://com.android.contacts/contacts/293/photo

这是我的getViewAt 方法:

// Create and populate the View to display at the given index.
    public RemoteViews getViewAt(int index) {

        // Create a view to display at the required index.
        RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.item_layout);

        // Populate the view with contact name.
        rv.setTextViewText(R.id.name, items.get(index).getString("first_name"));


        //check if we have a photo set
        if (items.get(index).getString("photo_url") != null) {

            Log.i("parsed uri", String.valueOf(Uri.parse(items.get(index).getString("photo_url"))));

            rv.setImageViewUri(R.id.photo_url, Uri.parse(items.get(index).getString("photo_url")));
        }

        // Create an item-specific fill-in Intent that will populate
        // the Pending Intent template created in the App Widget Provider (GridWidgetProvider).
        Intent fillInIntent = new Intent();
        fillInIntent.putExtra(Intent.EXTRA_TEXT, items.get(index));
        rv.setOnClickFillInIntent(R.id.name, fillInIntent);
        return rv;
}

联系人姓名显示正常,但没有图片...

知道为什么吗?谢谢

编辑:ImageView 的 xml:

<ImageView
    android:id="@+id/photo_url"
    android:layout_width="match_parent"
    android:layout_height="125dp"
    android:layout_above="@id/name"
    android:gravity="center"
    android:background="#FFFF0000"
/>

【问题讨论】:

    标签: android-widget android-contacts


    【解决方案1】:

    自己搞定的,代码如下:

    //check if we have a photo set
            if (items.get(index).getString("photo_url") != null) {
    
                rv.setImageViewUri(R.id.photo_url, Uri.parse(items.get(index).getString("photo_url")));
            }
    
            Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, items.get(index).getLong("contact_id"));
            InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), contactUri);
    
            if (input != null) {
    
                Log.i("input", String.valueOf(input));
    
                image = BitmapFactory.decodeStream(input);
    
            } else {
                image = BitmapFactory.decodeResource(context.getResources(), R.drawable.default_dark);
            }
    
            rv.setImageViewBitmap(R.id.photo_url, image);
    

    【讨论】:

      猜你喜欢
      • 2013-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-10
      • 1970-01-01
      • 1970-01-01
      • 2013-06-29
      相关资源
      最近更新 更多