【问题标题】:Uploading a photo to Firebase Storage and updating the profile aatar将照片上传到 Firebase 存储并更新个人资料头像
【发布时间】:2020-03-16 13:20:50
【问题描述】:

Hello So basically when clicking on the image view the dialog opens and i can choose a picture however when the picture is selected the app get's me disconnected from the account without uploading and updating the picture .

 dialog = new SpotsDialog.Builder(getContext()).create();
    storageReference=FirebaseStorage.getInstance().getReference("image_upload");

    avatarIv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent =new Intent();
            intent.setType("image/");
            intent.setAction(intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent,"Choisissez une image"),PICK_IMAGE_CODE);
        }
    });



 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == PICK_IMAGE_CODE ){
        dialog.show();
        UploadTask uploadTask =storageReference.putFile(data.getData());
        Task<Uri>task= uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
            @Override
            public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                if (!task.isSuccessful()){
                    Toast.makeText(getActivity(),"Erreur de téléchargement",Toast.LENGTH_SHORT).show();

                }
                return  storageReference.getDownloadUrl();
            }
        }).addOnCompleteListener(new OnCompleteListener<Uri>() {
            @Override
            public void onComplete(@NonNull Task<Uri> task) {
                if(task.isSuccessful()){
                    String Url =task.getResult().toString().substring(0,task.getResult().toString().indexOf("&token"));
                    Log.d("DIRECTLINK",Url);
                    Picasso.get().load(Url).into(avatarIv);
                    dialog.dismiss();
                }
            }
        });
    }
}

通过在 if(requestCode == PICK_IMAGE_CODE ) 中使用断点 我得到了这个

Debug LOG

Ps:这段代码是在一个片段中实现的(底部导航视图)

私有静态最终 int PICK_IMAGE_CODE = 1000;

编辑:这些是存储规则 允许读、写:if request.auth != null;

【问题讨论】:

    标签: java firebase android-fragments firebase-storage android-studio-2.3


    【解决方案1】:

    根据您在此处发布的堆栈跟踪判断:

    您似乎遇到了类路径/类加载器问题。确保您对所有 Firebase 库使用相同的版本。检查你的类路径。

    看到这个similar question

    【讨论】:

    • 所以我将 firebase 存储实现更改为与登录和数据库相同的级别,应用程序不再崩溃,但是我收到提示“下载错误”这里是新日志 imgur.com/a/Di3uPdg
    • 这是一个“权限被拒绝”的问题。您需要提出一个新问题!
    【解决方案2】:

    这对我有用

    match /b/{bucket}/o {
      match /{userId} {
        match /{allPaths=**}{
         allow read 
         allow write;
        }
      }
    }
    

    【讨论】:

    • 这条规则看起来很危险——它允许任何用户(包括未经身份验证的用户)在你的 Firebase 存储中随意读写——而且很容易被滥用。
    猜你喜欢
    • 2020-02-07
    • 2019-10-17
    • 1970-01-01
    • 2017-02-23
    • 1970-01-01
    • 2021-09-20
    • 2014-08-31
    • 2019-04-18
    • 2018-01-28
    相关资源
    最近更新 更多