使用Kotlin将图像从ImageView传递到Android中的另一个活动
如何使用使用Kotlin将图像从ImageView传递到Android中的另一个活动,android,image,android-intent,kotlin,Android,Image,Android Intent,Kotlin,如何使用Kotlin将在ImageView中选择的图像传递给Android中的其他活动 这是使用内部存储器在ImageView中选择图像的方法,我需要将图像传递给另一个活动 fun Loadimage() { var intent = Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI) startActivityForResu
Kotlin
将在ImageView
中选择的图像传递给Android中的其他活动
这是使用内部存储器在ImageView
中选择图像的方法,我需要将图像传递给另一个活动
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))
}
}
您可以使用下面的意图将
Picture
变量传递给下一个活动
val intent = Intent(this, NextActivity::class.java)
intent.putExtra("picture", Picture)
startActivity(intent)
然后在NextActivity的onCreate
方法中,您可以使用
val Picture = getIntent().getStringExtra("picture")
在
下一个触觉中
:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val postImage = findViewById<ImageView>(R.id.post_image)
myPic = postImage
}
companion object {
lateinit var myPic: ImageView()
}
不要将图像传递给其他活动,只传递该图像的路径,并在目标活动的
onCreate()
method中处理该路径以将文件转换为位图永远不要使用意图传递大型数据集。而是传递图像URI。@VincentmimounPrat图片只是URI。请看一下他的密码。Picture是用于存储imageCorrect的uri的变量。很抱歉除非编辑答案,否则无法恢复投票。
NextActivity.myPic = Picture