Retrieve image from Firebase Storage

社会主义新天地 提交于 2019-12-11 18:26:32

问题


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

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.


回答1:


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

let your activity implement IOnSuccessListener interface:

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

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

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



来源:https://stackoverflow.com/questions/55876667/retrieve-image-from-firebase-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!