Firebase存储PutFile Kotlin崩溃

时间:2019-12-03 13:39:28

标签: android firebase kotlin firebase-storage

我的目标是尝试将我从“照片库”或“摄像机”中选择的照片上传到Firebase存储中,该文件存储在名为ProfilePictures的存储桶/文件夹中。

起初一切工作都很好,我使用Picasso将图像分配给ImageView和Camera Intent,但调用uploadPhoto()时除外。该应用程序崩溃并在fileReference.putFile(imageUri)...上重新启动。

这是我的清单:

 <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="com.google.android.apps.photos.permission.GOOGLE_PHOTOS"/>
    <uses-feature android:name="android.hardware.camera"
            android:required="true" />

我正在使用Firebase Storage版本,该版本位于我的build.gradle文件中->实现“ com.google.firebase:firebase-storage:16.0.4”

这是我的代码:

    private var mStorageRef = FirebaseStorage.getInstance().getReference("profilePictures")
private fun uploadFile(name: String, callback: ()->Unit) {
        if (selectedProfileImage != null) {
            val imageUri = selectedProfileImage ?: return
            var fileReference = mStorageRef.child(name + "." + getFileExtension(imageUri))
            println(fileReference)
            fileReference.putFile(imageUri).addOnSuccessListener {
                    println("Profile Image upload Successful")
                    callback()
                }.addOnFailureListener {
                    println("Profile image upload failed")
                    println(it.stackTrace)
                    println(it.localizedMessage)
                    callback()
                }

        } else {
            println("No profile picture selected")
            callback()
        }
    }

这是我的堆栈跟踪:

2019-12-03 14:23:34.521 7580-7761/com.fisherassociates.mt18academy E/AndroidRuntime: FATAL EXCEPTION: FirebaseStorage-Upload-1
    Process: com.fisherassociates.mt18academy, PID: 7580
    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.fisherassociates.mt18academy-CsL1s9aKJKR7OHyF2sQSHw==/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(Unknown Source:2)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:919)

1 个答案:

答案 0 :(得分:0)

问题已解决。我使用Android Studio中的Firebase工具来设置存储,然后将build.gradle应用文件中的实现添加到版本16.0.4。

找到-> java.lang.NoSuchMethodError: No virtual method setTokenProvider

然后,我查看了Firebase Android发行说明: https://firebase.google.com/support/release-notes/android

通过将版本更改为19.1.0,代码现在可以使用了。

实现'com.google.firebase:firebase-storage:19.1.0'