0

I am trying to build a simple app that passes objects between activities so I could use the Class properties in the recyclerview later on. I have created the "Member data class" and extended it so that it uses the parelable interface.

data class Member(val name : String, val image : ByteArray) : Parcelable {
constructor(parcel: Parcel) : this(
    parcel.readString(),
    parcel.createByteArray()
) {
}

override fun writeToParcel(parcel: Parcel, flags: Int) {
    parcel.writeString(name)
    parcel.writeByteArray(image)
}

override fun describeContents(): Int {
    return 0
}

companion object CREATOR : Parcelable.Creator<Member> {
    override fun createFromParcel(parcel: Parcel): Member {
        return Member(parcel)
    }

    override fun newArray(size: Int): Array<Member?> {
        return arrayOfNulls(size)
    }
}

}

After that i have created a memberCreation activity to input some data in the class and pass the class back to the main activity using intent.

class MemberCreation : AppCompatActivity() {

private lateinit var profilePic : ImageView
private lateinit var nameEditText: EditText
private lateinit var saveButton: Button
private lateinit var profilePictureByteArray: ByteArray
private lateinit var member: Member

companion object{
    const val REQUES_CODE_GALLERY = 999
    const val EXTRA_REPLY = "robybp.coolkids.reply.extra"
}

@RequiresApi(Build.VERSION_CODES.JELLY_BEAN)
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_member_creation)

    profilePic = findViewById(R.id.profile_picture)
    nameEditText = findViewById(R.id.name_edit_text)
    saveButton = findViewById(R.id.button_save)

    profilePic.setOnClickListener {
        ActivityCompat.requestPermissions(this,
            arrayOf(android.Manifest.permission.READ_EXTERNAL_STORAGE),
            REQUES_CODE_GALLERY)
    }

    saveButton.setOnClickListener {
        if (nameEditText.text.isNotEmpty()){
            val activityIntet = Intent()
            val name : String = nameEditText.text.toString()
            member = Member(name, profilePictureByteArray)
            activityIntet.putExtra(EXTRA_REPLY, member)
            setResult(Activity.RESULT_OK, activityIntet)
            finish()
        }
    }
}

override fun onRequestPermissionsResult(
    requestCode: Int,
    permissions: Array<out String>,
    grantResults: IntArray
) {
    if (requestCode == REQUES_CODE_GALLERY){
        if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            val intent = Intent(Intent.ACTION_PICK)
            intent.type = "image/*"
            startActivityForResult(intent, REQUES_CODE_GALLERY)
        }else{
            Toast.makeText(this, "You don't have the permission to access gallery", Toast.LENGTH_LONG).show()
        }
    }
    super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if (requestCode == REQUES_CODE_GALLERY && resultCode == Activity.RESULT_OK && data!= null) run {
        val uri: Uri = data.data
        try {
            val inputStream: InputStream = contentResolver.openInputStream(uri)
            val bitmap : Bitmap = BitmapFactory.decodeStream(inputStream)
            profilePic.setImageBitmap(bitmap)
            profilePictureByteArray = imageToArray(profilePic)

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

    }
    super.onActivityResult(requestCode, resultCode, data)
}

private fun imageToArray(image : ImageView) : ByteArray{
    val bitmap = (image.drawable as BitmapDrawable).bitmap
    val stream = ByteArrayOutputStream()
    bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream)
    val image = stream.toByteArray()
    return image
}

}

But when i go back to retrieve the data in my main activity the app simply crashes without any logcat warnings. Also I am aware that I need to choose the profile picture from the gallery or the app will crash. I plan on adding a default value to the profilePictureByteArray later.

Here is a snippet of the code from my Main activity where i retrieve the object.

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == MEMBER_CREATION_REQUEST_CODE && resultCode == Activity.RESULT_OK){
        val member : Member = data!!.getParcelableExtra(MemberCreation.EXTRA_REPLY)
    }
}

I would like to point out that I sort of a beginner so I apologize if the question is really simple.

  • "But when i go back to retrieve the data in my main activity the app simply crashes without any logcat warnings" -- if it crashes, then I guarantee that there will be a message in Logcat. Note that it might be for a separate process -- you would need to choose that "[DEAD]" process in the process drop-down in the Logcat tool. – CommonsWare Feb 23 '20 at 16:11
  • Okay now I have gotten: Unknown bits set in runtime_flags: 0x8000 I don't really know what that means and didn't get much information online (At least that I considered relevant to my problem) – Robert Baricevic-Petrus Feb 23 '20 at 16:14
  • That is not [a stack trace](https://stackoverflow.com/q/3988788/115145), and that is a common message that is unrelated to your problem. See also https://stackoverflow.com/q/23353173/115145 – CommonsWare Feb 23 '20 at 16:17

0 Answers0