Android Firebase上传图片

时间:2016-12-05 19:07:23

标签: android firebase firebase-storage

我尝试使用以下代码将图像上传到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服务。我的手机已经有了最新版本。我还尝试启用匿名登录并公开规则。

还有其他建议吗?

1 个答案:

答案 0 :(得分:0)

我是个白痴......

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

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