1
votes

How to pass Image selected in ImageView to another Activity in Android using Kotlin ?

This is the way to select the Image inside the ImageView using internal Storage and I need to pass the image to another activity

 fun Loadimage()
    {
         var intent = Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
         startActivityForResult(intent,ImageCode)
     }

     override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
         if (requestCode==ImageCode && data!=null && resultCode== Activity.RESULT_OK)
         {
             val selectedImage = data.data
             val filepath = arrayOf(MediaStore.Images.Media.DATA)
             val cursor = contentResolver.query(selectedImage,filepath,null,null,null)
             cursor.moveToFirst()
             val Index = cursor.getColumnIndex(filepath[0])
             val Picture = cursor.getString(Index)
             cursor.close()
             imageView.setImageBitmap(BitmapFactory.decodeFile(Picture))
         }
     }
2
do not pass the image to other activities, pass just path to that image, and procceed that path to convert a file into bitmap inside target activity's onCreate() method - Irony Stack

2 Answers

2
votes

You can pass Picture variable to next activity using Intents like below

val intent = Intent(this, NextActivity::class.java)
intent.putExtra("picture", Picture)
startActivity(intent)

Then in NextActivity, in onCreate method, you can get picture using

 val Picture = getIntent().getStringExtra("picture")
0
votes

In the NextActivity:

 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val postImage = findViewById<ImageView>(R.id.post_image)
    myPic = postImage
}

companion object {
    lateinit var myPic: ImageView()
}

And in the first Activity:

NextActivity.myPic = Picture