Jim
Jim

Reputation: 63

Retrieve image from Firebase Storage

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.

Upvotes: 0

Views: 593

Answers (1)

Leo Zhu
Leo Zhu

Reputation: 14956

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

Upvotes: 1

Related Questions