【问题标题】:Storing Images in Firebase with Authentication ID- Xamarin使用身份验证 ID-Xamarin 在 Firebase 中存储图像
【发布时间】:2017-07-05 17:10:06
【问题描述】:

我可以将图像保存到我的 firebase 存储中,但是在存储中,没有什么可以告诉我上传照片的用户的 ID。在我的应用程序中,用户可以为博客上传照片,并且就这一点而言,每张更新的照片都应该有用户的 id 和博客的密钥。所以我很容易为每个博客分别获取图像。我怎样才能做到这一点?

这就是我保存图像的方式

 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)
        {

        }

【问题讨论】:

    标签: c# android firebase xamarin.android firebase-storage


    【解决方案1】:

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

    我假设您在StorageReference images = storageRef.Child("images"); 中的storageRef 表示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");
    

    【讨论】:

      猜你喜欢
      • 2021-07-30
      • 2020-12-16
      • 2018-10-11
      • 2020-06-26
      • 1970-01-01
      • 2017-06-07
      • 2019-06-28
      • 2017-03-16
      • 1970-01-01
      相关资源
      最近更新 更多