2017-07-04 66 views
0

我能够将图像保存到我的Firebase存储中,但在存储中,没有任何内容可以告诉我上传照片的用户的ID。在我的应用程序中,用户可以上传照片作为博客,尽可能多地更新每张照片应该分别具有用户的ID和博客的关键。因此,我很容易分别为每个博客获取图像。我怎样才能做到这一点?使用身份验证ID将图像存储在Firebase中 - Xamarin

这是我如何保存我的图片

public void OnClick(View v) 
     { 
      Intent intent = new Intent(); 
      intent.SetType("image/*"); 
      intent.SetAction(Intent.ActionPick); 
      StartActivityForResult(Intent.CreateChooser(intent, "Select Image"), PICK_IMAGE_REQUEST); 

     } 

     protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data) 
     { 
      base.OnActivityResult(requestCode, resultCode, data); 

      try{ 
       Bitmap bitmap = MediaStore.Images.Media.GetBitmap(this.ContentResolver, data.Data); 
       using (MemoryStream stream = new MemoryStream()) 
       { 
        bitmap.Compress(Bitmap.CompressFormat.Jpeg,100,stream); 
        byte[] array = stream.ToArray(); 

        imageViewDrop.SetImageBitmap(bitmap); 


       } 

       if(data.Data != null){ 
        pd = new ProgressDialog(this); 
        pd.SetTitle("Uploading"); 
        pd.Show(); 

        StorageReference images = storageRef.Child("images"); 

        images.PutFile(data.Data).AddOnSuccessListener(this); 
        images.PutFile(data.Data).AddOnProgressListener(this); 
       } 


       else { 

       } 


       imageViewDrop.SetImageBitmap(bitmap); 

      } 
      catch (Java.Lang.Exception e){ 

      } 


     } 


     public void OnSuccess(Java.Lang.Object result) 
     { 
      pd.Dismiss(); 
     } 

     public void OnProgress(Java.Lang.Object snapshot) 
     { 

     } 

回答

1

您可以使用用户的Uid创建为每个用户在存储的参考。

我假设你storageRefStorageReference images = storageRef.Child("images");意味着FirebaseStorage.Instance.Reference,那么首先检查当前用户是这样的:

FirebaseAuth mAuth = FirebaseAuth.Instance; 
FirebaseUser user = mAuth.CurrentUser; 

如果用户是空的,当然我们需要进行身份验证,但如果它不为空,创建用户参考,然后根据此参考创建您的“图像”或“博客”等...例如:

FirebaseStorage storage = FirebaseStorage.Instance; 
//create child reference 
StorageReference storageRef = storage.Reference; 

var userRef = storageRef.Child(user.Uid); 
var imageRef = userRef.Child("images");