将图像发送到 Firebase 存储时应用程序崩溃

App crashes when sending image to Firebase storage

所以在我的应用程序中,我需要个人资料图片,但是当我 select 一个时,我的应用程序崩溃了。我添加了所有 sdk,也尝试通过添加 Google 照片的权限来修复它,但这没有帮助。我真的不知道是什么导致了这个问题我还添加了我的桶 url 这样就不会出现问题或者我做错了吗?这是我的代码。

图片存储函数:

    fun storeImage() {
        if (resultImageUrl != null && userId != null){
            val filePath = FirebaseStorage.getInstance("gs://dogsharev2-5e4c3.appspot.com").reference.child("profileImage").child(userId)
            var bitmap: Bitmap? = null
            try {
                if (android.os.Build.VERSION.SDK_INT >= 29){
                    // To handle deprecation use
                    val source = ImageDecoder.createSource(contentResolver,resultImageUrl!!)
                    bitmap = ImageDecoder.decodeBitmap(source)
                } else {
                    // Use older version
                    bitmap = MediaStore.Images.Media.getBitmap(application.contentResolver, resultImageUrl!!)

                }
            }catch (e: IOException){
                e.printStackTrace()
            }

            val baos = ByteArrayOutputStream()
            bitmap?.compress(Bitmap.CompressFormat.JPEG, 200, baos)
            val data = baos.toByteArray()

            val uploadTask = filePath.putBytes(data)
            uploadTask.addOnFailureListener {e -> e.printStackTrace()}
            uploadTask.addOnSuccessListener { taskSnapshot ->
                filePath.downloadUrl
                    .addOnSuccessListener { uri ->
                        profileFragment?.updateImageUri(uri.toString())
                    }
                    .addOnFailureListener { e -> e.printStackTrace()}
            }
        }
    }

启动图像的函数activity:

    override fun startActivityForPhoto() {
        val intent = Intent(Intent.ACTION_PICK)
        intent.type = "image/*"
        getResult.launch(intent)
    }
    private val getResult =
        registerForActivityResult(
            ActivityResultContracts.StartActivityForResult()
        ){
            if (it.resultCode == Activity.RESULT_OK){
                resultImageUrl = it.data?.data
                storeImage()
            }
        }

我也遇到了错误:

2022-05-19 23:21:38.333 26824-26824/com.example.dogsharev2 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.dogsharev2, PID: 26824
    java.lang.IllegalArgumentException: quality must be 0..100
        at android.graphics.Bitmap.compress(Bitmap.java:1436)
        at com.example.dogsharev2.activities.TinderActivity.storeImage(TinderActivity.kt:155)
        at com.example.dogsharev2.activities.TinderActivity.getResult$lambda-0(TinderActivity.kt:132)
        at com.example.dogsharev2.activities.TinderActivity.$r8$lambda$jtOlPDH_wItpCc5GiEy42Z8DYno(Unknown Source:0)
        at com.example.dogsharev2.activities.TinderActivity$$ExternalSyntheticLambda0.onActivityResult(Unknown Source:4)
        at androidx.activity.result.ActivityResultRegistry.onStateChanged(ActivityResultRegistry.java:148)
        at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:354)
        at androidx.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:265)
        at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:307)
        at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:148)
        at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:134)
        at androidx.lifecycle.ReportFragment.dispatch(ReportFragment.java:68)
        at androidx.lifecycle.ReportFragment$LifecycleCallbacks.onActivityPostStarted(ReportFragment.java:187)
        at android.app.Activity.dispatchActivityPostStarted(Activity.java:1362)
        at android.app.Activity.performStart(Activity.java:8061)
        at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3475)
        at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
        at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

提前致谢。

如果您查看堆栈跟踪的前几行,您会发现一些事情

java.lang.IllegalArgumentException: quality must be 0..100
        at android.graphics.Bitmap.compress(Bitmap.java:1436)
        at com.example.dogsharev2.activities.TinderActivity.storeImage(TinderActivity.kt:155)
  1. 错误是因为一些名为“质量”的参数应该介于 0 和 100 之间
  2. 在Bitmap.compress
  3. 中抛出错误
  4. 该方法是从您的 storeImage 函数调用的

这会将您指向这一行:

bitmap?.compress(Bitmap.CompressFormat.JPEG, 200, baos)

因此,解决方案是将质量参数从 200 更改为 0 到 100 之间的某个数字(100 是最高 quality/lowest 压缩,0 是最高 compression/lowest 质量)。