从Firebase存储检索图像 [英] Retrieve image from Firebase Storage

查看:72
本文介绍了从Firebase存储检索图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Firebase存储中提取图像,以便随后可以使用图像的URL放入Imageview.我发现的大多数答案都与android studio有关,而xamarin则没有答案.这是我到目前为止的代码:

I am trying to pull an image from Firebase storage so i can then use the URL of the image to put into an Imageview. Most of the answers i find are to do with android studio, no answers for xamarin. This is the code i have so far:

storage = FirebaseStorage.Instance;
storageRef = storage.GetReferenceFromUrl("gs://......com");

StorageReference ImageFolder = storageRef.Child("Images");
StorageReference UserFolder = ImageFolder.Child(auth.CurrentUser.Email);
StorageReference UserImage = storageRef.Child("profile pic");

我可以上传到Firebase存储没有问题,它的检索部分就是问题.在Firebase存储中,我有一个文件夹Images/userEmail/profilePic.

I can upload to firebase storage no problem, its the retrieving part that is the problem. In firebase storage i have folder Images/userEmail/profilePic.

任何链接或文档都会有人指出我的方向,否则任何帮助将非常感谢.

Any links or documentation someone could point me in the direction of or any help would be great thanks.

推荐答案

您可以实现 IOnSuccessListener 接口并通过 OnSuccess 方法获取结果像这样:

you could implement the IOnSuccessListener interface and get tht result int the OnSuccess method like this:

让您的活动工具实现 IOnSuccessListener 接口:

let your activity implement IOnSuccessListener interface:

public class YourActivity: Activity, IOnSuccessListener, IOnFailureListener
 {
  ...
 }

使用 Android.Gms.Tasks.Task 下载文件并在 OnSuccess 回调中设置为imageview:

use Android.Gms.Tasks.Task download the file and set into imageview in the OnSuccess call back:

StorageReference ImageFolder = storageRef.Child("Images");
StorageReference UserFolder = ImageFolder.Child(auth.CurrentUser.Email);
StorageReference UserImage = UserFolder.Child("profile pic");
StorageReference testRef = UserImage.Child("test.jpg");
Task downloadtask = testRef.GetBytes(1200 * 800);
downloadtask.AddOnSuccessListener(this);
downloadtask.AddOnFailureListener(this);

public void OnFailure(Java.Lang.Exception e)
 {
    Log.WriteLine(LogPriority.Debug, "storage", "Failed:" + e.ToString());
 }

public void OnSuccess(Java.Lang.Object result)
 {
    Log.WriteLine(LogPriority.Debug, "storage", "success!");
    if (downloadtask != null)
    {
        var data = downloadtask.Result.ToArray<byte>();
        Bitmap bitmap = BitmapFactory.DecodeByteArray(data, 0, data.Length);
        imageview.SetImageBitmap(bitmap);
        downloadtask = null;
    }
 }

有几种方法可以做到这一点
您可以参考 FireBase

there are several ways to do this
you could refer to the FireBase

这篇关于从Firebase存储检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆