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

发布于 2025-01-31 03:51:13 字数 4846 浏览 3 评论 0原文

因此,在我的应用程序中,我需要个人资料映像,但是当我选择一个时,它会崩溃我的应用程序。我还添加了所有SDK,也尝试通过添加Google Photo的权限来修复它,但这无济于事。我真的不知道是什么原因导致了这个问题,我还添加了我的水桶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()}
            }
        }
    }

启动图像活动的函数:

    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$1.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)

谢谢。

So in my app i need profile images but when i select one it crashes my app. I added all the sdk's also tried fixing it by adding permission for Google photo's but that didn't help. I really have no idea what could have caused this problem I also added my bucket url so that doesn't give a problem or maybe I did this wrong? Here is my code.

Function to store the image:

    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()}
            }
        }
    }

function to start image 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()
            }
        }

I also got the errors :

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$1.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)

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

翻身的咸鱼 2025-02-07 03:51:13

您可以发现一些错误

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. 如果您查看堆栈跟踪的前几行,
  3. 之间您的storeImage函数

这将您指向这一行:

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

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

If you look at the first few lines of the stack trace you can find out a few things

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. The error is because some parameter called "quality" is supposed to be between 0 and 100
  2. The error is thrown in Bitmap.compress
  3. That method is called from your storeImage function

This points you to this line:

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

So, the solution is to change the quality argument there from 200 to some number between 0 and 100 (100 being the highest quality/lowest compression, 0 being the highest compression/lowest quality).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文