将图像添加到 Firebase 存储时出错
Posted
技术标签:
【中文标题】将图像添加到 Firebase 存储时出错【英文标题】:Error in adding image to firebase storage 【发布时间】:2020-07-21 14:33:49 【问题描述】:我正在使用 kotlin/android 和 firebase 存储,在我想将图像上传到 firebase 存储之前,它运行良好。但最近我得到了错误。 我在***中搜索了很多,我发现这个问题与我的相似,但没有任何答案:
StorageException has occurred. Unable to upload images in firebase
这是我的错误:
E/StorageException: StorageException has occurred.
An unknown error occurred, please check the HTTP result code and inner exception for server response.
Code: -13000 HttpResult: 0
StorageException has occurred.
An unknown error occurred, please check the HTTP result code and inner exception for server response.
Code: -13000 HttpResult: 0
E/AndroidRuntime: FATAL EXCEPTION: FirebaseStorage-Upload-1
Process: com.example.bestplaceapp, PID: 20236
java.lang.NoSuchMethodError: No virtual method getToken(Z)Lcom/google/android/gms/tasks/Task; in class Lcom/google/firebase/FirebaseApp; or its super classes (declaration of 'com.google.firebase.FirebaseApp' appears in /data/app/com.example.bestplaceapp-1/base.apk)
at com.google.firebase.storage.internal.Util.getCurrentAuthToken(com.google.firebase:firebase-storage@@16.0.4:148)
at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(com.google.firebase:firebase-storage@@16.0.4:65)
at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(com.google.firebase:firebase-storage@@16.0.4:57)
at com.google.firebase.storage.UploadTask.sendWithRetry(com.google.firebase:firebase-storage@@16.0.4:457)
at com.google.firebase.storage.UploadTask.beginResumableUpload(com.google.firebase:firebase-storage@@16.0.4:257)
at com.google.firebase.storage.UploadTask.run(com.google.firebase:firebase-storage@@16.0.4:198)
at com.google.firebase.storage.StorageTask.lambda$getRunnable$7(com.google.firebase:firebase-storage@@16.0.4:1106)
at com.google.firebase.storage.StorageTask$$Lambda$12.run(com.google.firebase:firebase-storage@@16.0.4)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:762)
E/StorageException: StorageException has occurred.
An unknown error occurred, please check the HTTP result code and inner exception for server response.
Code: -13000 HttpResult: 0
我的代码以前可以运行,这是我的代码:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?)
super.onActivityResult(requestCode, resultCode, data)
if(requestCode == GALLERY_ID && resultCode == Activity.RESULT_OK)
var image: Uri = data?.data!!
// We crop the image
CropImage.activity(image)
.setAspectRatio(1, 1)
.start(this)
if(requestCode === CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)
val result = CropImage.getActivityResult(data)
if(resultCode === Activity.RESULT_OK)
// preparing image
var resultUri = result.uri
mCurrentUser = FirebaseAuth.getInstance().currentUser
var userId = mCurrentUser!!.uid
var thumbFile = File(resultUri.path)
// We compress the image and save in thumbnail field in database
var thumbBitmap = Compressor(this)
.setMaxHeight(200)
.setMaxWidth(200)
.setQuality(65)
.compressToBitmap(thumbFile)
//We upload images to firebase
var byteArray = ByteArrayOutputStream()
thumbBitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArray)
var thumbByteArray: ByteArray
thumbByteArray = byteArray.toByteArray()
var filePath = mStorageRef!!.child("user_images")
.child(userId + ".jpg")
//Create another directory for thumbnail images ( smaller, compressed images )
var thumbFilePath = mStorageRef!!.child("user_images")
.child("thumbs")
.child(userId + ".jpg")
// end of preparing image
// put main image file to firebase storage
var uploadTask_MainImage = filePath.putFile(resultUri)
uploadTask_MainImage.continueWithTask task ->
if (!task.isSuccessful)
task.exception?.let
throw it
filePath.downloadUrl
.addOnCompleteListener task ->
if (task.isSuccessful)
val downloadUri = task.result.toString()
// put thumbnail image file to firebase storage
var uploadTask_ThumbnailImage: UploadTask = thumbFilePath
.putBytes(thumbByteArray)
uploadTask_ThumbnailImage.continueWithTask task ->
if (!task.isSuccessful)
task.exception?.let
throw it
thumbFilePath.downloadUrl
.addOnCompleteListener task ->
var thumbUrl = task.result.toString()
if (task.isSuccessful)
var updateObj = HashMap<String, Any>()
updateObj.put("image", downloadUri)
updateObj.put("thumb_image", thumbUrl)
//We save the download url of main image and thumbnail image to users table in database
mDatabase!!.updateChildren(updateObj)
.addOnCompleteListener task: Task<Void> ->
if (task.isSuccessful)
Toast.makeText(
this, "Profile image saved!",
Toast.LENGTH_LONG
).show()
else
// Handle failures
// TODO
else
// Handle failures
// TODO
我使用这些存储规则,在我的代码中,用户应该登录才能将图像保存到存储:
rules_version = '2';
service firebase.storage
match /b/bucket/o
match /allPaths=**
allow read, write: if request.auth != null;
【问题讨论】:
请分享您在 Firebase 存储中的规则 确保您在 build.gradle 中使用了所有最新版本的 Firebase 库。 【参考方案1】:您必须更改您的存储规则
rules_version = '2';
service firebase.storage
match /b/bucket/o
match /allPaths=**
allow read, write; //<- Change here
【讨论】:
【参考方案2】:亲爱的朋友我发现了我的问题,我的firebase-storage的版本号与firebase-database和firebase-auth的版本不兼容,我在gradle.build文件中更改了这样的版本:
implementation 'com.google.firebase:firebase-database:19.2.1'
implementation 'com.google.firebase:firebase-auth:19.3.0'
implementation 'com.google.firebase:firebase-storage:19.1.1'
它成功了:)
【讨论】:
以上是关于将图像添加到 Firebase 存储时出错的主要内容,如果未能解决你的问题,请参考以下文章
Firebase 存储上传图像不适用于亚马逊火灾(型号:Fire 7)
将图像上传到 Firebase 存储并同时将文本输入到 Firestore 时出错