【问题标题】:Uploading photo to firebase storage not working将照片上传到 Firebase 存储不起作用
【发布时间】:2018-02-06 10:06:25
【问题描述】:

当用户单击我的聊天应用程序中的某个按钮时,我正在尝试将图像上传到 Firebase 存储,但它不起作用。

这是上传代码

if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK) {

    Uri selectedImageUri = data.getData();
    StorageReference photoRef = mChatPhotosStorageReference.child(selectedImageUri.getLastPathSegment());

    photoRef.putFile(selectedImageUri)
            .addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
                 public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                     // When the image has successfully uploaded, we get its download URL
                     Uri downloadUrl = taskSnapshot.getDownloadUrl();

                     // Set the download URL to the message box, so that the user can send it to the database
                     FriendlyMessage friendlyMessage = new FriendlyMessage(null, mUsername, downloadUrl.toString());
                     mDatabaseReference.push().setValue(friendlyMessage);
                 }
            });
}

【问题讨论】:

  • 你遇到了什么错误?
  • 有错误吗?会不会是身份验证问题?
  • 在您的应用控制台中检查 Firebase 存储规则,如果您没有使用任何 Firebase 身份验证方法,那么您的规则必须允许对所有用户(不仅仅是经过身份验证的用户)进行读写才能上传或下载任何文件。
  • 我没有收到任何错误消息,它只是没有将任何内容上传到数据库。我不认为这是一个身份验证问题,普通短信可以正常工作,并且它具有与图像相同的规则“service firebase.storage { match /b/{bucket}/o { match /{allPaths=**} { allow read,写:如果 request.auth != null; } } }"
  • @tokaaliamien 它会上传到存储吗?

标签: android firebase firebase-storage


【解决方案1】:
public void uploadFile(Uri filePath, UploadImageInterface uploadImageInterface) {
StorageReference mStorageRef = FirebaseStorage.getInstance().getReferenceFromUrl(Constants.FIREBASE_STORAGE_PATH).child("images");
if (filePath != null) {
    StorageReference filepathRef = mStorageRef.child("pic" + CommonUtilities.getTimeStampLong() + ".JPG");
    filepathRef.putFile(filePath)
        .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                String imageUrl = taskSnapshot.getDownloadUrl() + "";
                uploadImageInterface.onSuccess(imageUrl);
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                uploadImageInterface.onFailure(exception.getMessage())       
            }
        });
    }
}

【讨论】:

  • 添加规则还需要检查你的存储路径
  • 添加规则为:- service firebase.storage { match /b/projectname-dfd60.appspot.com/o { match /{allPaths=**} { 允许读取,写入:if request.auth != 空; } } }
  • 以上配置对我有用。请说明您遇到的错误。
猜你喜欢
  • 2019-04-18
  • 2018-12-29
  • 2017-06-16
  • 2021-07-06
  • 2021-06-30
  • 2018-02-14
  • 2021-03-13
  • 2017-09-17
  • 2023-03-14
相关资源
最近更新 更多