iT邦幫忙

0

Xamarin Android 瀏覽所有已上傳至firebase 的照片

  • 分享至 

  • xImage

請問如何用Xamarin Android 寫出瀏覽所有已上傳至firebase 的照片

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
JamesDoge
iT邦高手 1 級 ‧ 2023-02-03 08:32:09
  1. 安裝Firebase UI
Install-Package Xamarin.Firebase.UI.Storage -Version 6.3.0
  1. 加入佈局文件RecyclerView
<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
  1. 在Activity中,建立FirebaseRecyclerAdapter:
FirebaseStorage storage = FirebaseStorage.Instance;
StorageReference storageRef = storage.GetReferenceFromUrl("gs://<your-firebase-storage-bucket>");

FirebaseRecyclerOptions<ImageModel> options = new FirebaseRecyclerOptions.Builder<ImageModel>()
    .SetQuery(storageRef, ImageModel.Deserializer)
    .Build();

FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<ImageModel, ImageViewHolder>(options)
{
    @Override
    public ImageViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
    {
        View view = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.image_item, parent, false);
        return new ImageViewHolder(view);
    }

    @Override
    protected void OnBindViewHolder(ImageViewHolder holder, int position, ImageModel model)
    {
        holder.Name.Text = model.Name;
        Glide.With(holder.ItemView.Context)
            .Load(model.ImageUrl)
            .Into(holder.Image);
    }
};

RecyclerView recyclerView = FindViewById<RecyclerView>(Resource.Id.recycler_view);
recyclerView.SetLayoutManager(new LinearLayoutManager(this));
recyclerView.SetAdapter(adapter);

  1. 建立ImageModel類別
public class ImageModel
{
    public static ImageModel Deserializer(DataSnapshot snapshot)
    {
        ImageModel imageModel = new ImageModel();
        imageModel.Name = snapshot.Key;
        imageModel.ImageUrl = snapshot.GetValue(JsonElement.class).GetAsString();
        return imageModel;
    }

    public string Name { get; set; }

    public string ImageUrl { get; set; }
}

僅供參考

我要發表回答

立即登入回答