Android Firebase上传图像



我尝试使用以下代码将图像上传到firebase存储:

    FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();
    final StorageReference storageRef = firebaseStorage.getReferenceFromUrl("gs://xxx");
    Uri file = Uri.fromFile(new File(url));
    mStorageRef = storageRef.child("images/"+file.getLastPathSegment());
    UploadTask uploadTask = storageRef.putFile(file);
    uploadTask.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Handle unsuccessful uploads
            Log.i(TAG, exception.toString());
        }
    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
            Uri downloadUrl = taskSnapshot.getDownloadUrl();
        }
    });

但是,我遇到以下错误: E/StorageException: StorageException has occurred. An unknown error occurred, please check the HTTP result code and inner exception for server response. Code: -13000 HttpResult: 400 E/StorageException: The server has terminated the upload session java.io.IOException: The server has terminated the upload session at com.google.firebase.storage.UploadTask.zzabc(Unknown Source) at com.google.firebase.storage.UploadTask.zzabb(Unknown Source) at com.google.firebase.storage.UploadTask.run(Unknown Source) at com.google.firebase.storage.StorageTask$8.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:818)

我发现有人建议在手机上更新Google Play服务。我的手机已经有最新版本。我还尝试了启用匿名登录并公开规则。

还有其他建议吗?

我真是个白痴...

应该 UploadTask uploadTask = mStorageRef.putFile(file);

而不是 UploadTask uploadTask = storageRef.putFile(file);

相关内容

  • 没有找到相关文章

最新更新