我正在使用kotlin / android和firebase存储,当我想将图像上传到firebase存储之前,它运行良好。但最近我得到了错误。 我在stackoverflow中进行了大量搜索,发现这个问题与我的类似,但没有任何答案:
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;
}
}
}
答案 0 :(得分:0)
您必须更改存储规则
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write; //<- Change here
}
}
}
答案 1 :(得分:0)
亲爱的朋友,我发现了我的问题,我的firebase-storage的版本号与firebase-database和firebase-auth的版本不兼容,我更改了gradle.build文件中的版本,如下所示:
phpversion.php - .php - 15 - 04/21/2020 09:56:52 - 04/21/2020 09:56:53
services.htm - .htm - 12881 - 04/21/2020 09:50:07 - 04/21/2020 09:55:07
它起作用了:)