【问题标题】:reference firebase storage from database to load image with FirebaseRecyclerAdapter从数据库引用 Firebase 存储以使用 FirebaseRecyclerAdapter 加载图像
【发布时间】:2018-02-03 15:55:13
【问题描述】:

我正在尝试复制 messageTxt.setText(downloadUrl.toString()); 这行代码所做的 Link here: (one of I/O 2016 demo)。它为 EditText 小部件提供与存储在 firebase 存储中的图像相关联的 url,并将其加载为图像。 Live demo,28:37播放。

就我而言,我打算使用 firebaseui 的 FirebaseRecyclerAdapter 将图像和其他数据(例如图像描述)加载到 recyclerview 上。我已经将数据保存到 firebase 数据库中,如下图所示。如您所见,其中一个值是 image_path,它保存了 firebase 存储中相应图像的 url。我使用taskSnapshot.getDownloadUrl(); 获取图片网址。

但是,当我尝试使用此代码将此图像 url 加载到 TextView 时,

public void setImage_path(String image) {
            feed_image = mView.findViewById(R.id.feed_image);
            feed_image.setText(image);
        }

仅显示文本,并且我希望显示图像的位置显示为空白。我该如何解决这个问题?提前致谢!我如何实现它的完整代码如下。让我知道是否需要任何其他代码,例如我的模型类。

代码:

Feed 片段

public class Feed extends Fragment {

    private Button upload;
    private String businessID;
    private RecyclerView rv;
    private DatabaseReference mDatabase;
    private static StorageReference mStorage;

    private FirebaseRecyclerAdapter<ImageInformation, ImageHolder> mFirebaseAdapter;

    public Feed() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        mStorage = FirebaseStorage.getInstance().getReference("feed_photos");
       ...
    }

    @Override
    public void onStart() {
        super.onStart();

        mFirebaseAdapter = new FirebaseRecyclerAdapter<ImageInformation, ImageHolder>(
                ImageInformation.class,
                R.layout.card_item_feed,
                ImageHolder.class,
                mDatabase) {

            @Override
            public void populateViewHolder(ImageHolder holder, ImageInformation chat, int position) {
                Glide.with(Feed.this).load(chat.getImage_path()).into(holder.feed_image);
            holder.image_desc.setText(chat.getCaption());
            holder.date.setText(chat.getDate_created());
            holder.tag.setText(chat.getTags());
            }
        };
        rv.setAdapter(mFirebaseAdapter);
    }

    public static class ImageHolder extends RecyclerView.ViewHolder{

    ImageView feed_image;
    TextView image_desc;
    TextView date;
    TextView tag;

    public ImageHolder(View v) {
        super(v);
        feed_image = v.findViewById(R.id.feed_image);
        image_desc = v.findViewById(R.id.image_desc);

        date = v.findViewById(R.id.date);
        tag = v.findViewById(R.id.tags);
    }
}
}

卡片视图 XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="-3dp"
    android:layout_marginRight="-3dp"
    card_view:cardBackgroundColor="@android:color/white"
    card_view:cardCornerRadius="0dp"
    card_view:cardUseCompatPadding="false"
    card_view:contentPadding="0dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/linear_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ImageView
            android:id="@+id/feed_image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="0dp"
            android:adjustViewBounds="true"
            android:visibility="visible" />

        </LinearLayout>

        <TextView
            android:id="@+id/image_desc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/linear_layout"/>

        <TextView
            android:id="@+id/date"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/linear_layout"/>

        <TextView
            android:id="@+id/tags"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/linear_layout"/>

    </RelativeLayout>

</android.support.v7.widget.CardView>

编辑:我意识到我错过了我的 Firebase 存储参考。我已经添加了它,但仍然卡住了,因为我找不到如何检索 Uri(selectedImageUri 代表演示中的 uri 变量)。 (参见 Feed Fragment 中的 cmets)

编辑 2: - 更改了 ImageHolder 类 - 更新了 populateViewHolder 方法中的内容 - 更新了卡片视图小部件

【问题讨论】:

  • feed_image textView 不应该是 ImageView 吗?
  • @UmarHussain,是的,你是对的。我已经更新了我的问题。

标签: android firebase-realtime-database firebase-storage android-recyclerview firebaseui


【解决方案1】:

首先你应该有

compile 'com.squareup.picasso:picasso:2.5.2'

从 firebase 获取图像的库,以任何你想要的方式展示。

@Override
            public void populateViewHolder(ImageHolder holder, ImageInformation chat, int position) {
                //holder.setImage_path(chat.getImage_path());
                 Picasso.with(this).load(chat.getImage_path()).into(holder.feed_image);
                 holder.title.setText(chat.getCaption());
               // holder.setCaption(chat.getCaption());
            }
        };

更多详情请点击链接Here查看示例

【讨论】:

  • 抱歉忘记在你的情况下改变它是持有者而不是 viewHolder 我建议你看看这个例子它有我认为你需要的所有代码
  • Here 我的一个应用程序使用相同的代码从firebase获取图像链接以显示在recyclerview中
  • 不走运,什么都没有加载。您是否使用了相同的参考资料?
  • 是的,我根据需要添加了一些,您也可以删除或添加到它
  • feed_image = (ImageView) v.findViewById(R.id.feed_image);试试这个
猜你喜欢
  • 2017-09-07
  • 2016-12-22
  • 2020-06-28
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 2019-05-29
  • 2017-03-06
  • 2020-11-06
相关资源
最近更新 更多