Home > Blockchain >  App crashes when sending image to Firebase storage
App crashes when sending image to Firebase storage

Time:05-22

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.

CodePudding user response:

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

  • Related