我使用定义为像素2的模拟器,并使用图像来自图库并由模拟器拍摄。权限部分工作正常,但是选择图像后,在imageView中没有设置图像。我已经尝试过在线搜索并调整了我的原始代码,例如将setImageURI更改为ImageDecode以创建位图。我是科特林的新蜂,所以您能帮我吗? 这是我的xml文件和代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PostActivity">
<include
android:id="@+id/tool_bar"
layout="@layout/toolbar"/>
<EditText
android:id="@+id/text_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/image_button"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="100dp"
android:layout_marginRight="20dp"
android:background="@drawable/input_outline"
android:padding="15dp"
android:hint="@string/post_title"/>
<EditText
android:id="@+id/text_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/text_title"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="50dp"
android:layout_marginRight="20dp"
android:background="@drawable/input_outline"
android:padding="15dp"
android:ems="10"
android:gravity="start|top"
android:inputType="textMultiLine"
android:hint="@string/post_description"/>
<Button
android:id="@+id/button_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/colorPrimary"
android:text="@string/post_submit" />
<ImageView
android:id="@+id/image_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tool_bar"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
app:srcCompat="@mipmap/add_btn" />
</RelativeLayout>
class PostActivity : AppCompatActivity() {
private var imageButton: ImageView?= null
// private val GALLERY_REQUEST = 1
companion object {
private val PERMISSION_CODE = 1000
private val IMAGE_PICK_CODE = 1001
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_post)
setSupportActionBar(findViewById(R.id.tool_bar))
imageButton = findViewById<ImageView>(R.id.image_button)
imageButton!!.setOnClickListener {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
var permissions= arrayOf(android.Manifest.permission.READ_EXTERNAL_STORAGE)
requestPermissions(permissions, PERMISSION_CODE)
} else {
pickImageFromGallery()
}
} else {
pickImageFromGallery()
}
}
}
private fun pickImageFromGallery() {
val intent = Intent(Intent.ACTION_PICK)
intent.type = "image/*"
startActivityForResult(intent, IMAGE_PICK_CODE)
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
PERMISSION_CODE -> {
if (grantResults.isNotEmpty() && grantResults[0] ==PackageManager.PERMISSION_GRANTED) {
pickImageFromGallery()
} else {
Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show()
}
}
}
}
@RequiresApi(Build.VERSION_CODES.P)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == IMAGE_PICK_CODE && requestCode== Activity.RESULT_OK) {
// imageButton?.setImageURI(data?.data)
if (data != null) {
val contentURI = data.data
try{
val source = ImageDecoder.createSource(this.contentResolver, contentURI!!)
val bitmap = ImageDecoder.decodeBitmap(source)
imageButton?.setImageBitmap(bitmap)
} catch(e: IOException) {
e.printStackTrace()
Toast.makeText(this, "Failed", Toast.LENGTH_SHORT).show()
}
}
}
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.post_menu, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.itemId
if (id == R.id.action_setting) {
val intent = Intent(this, SettingActivity::class.java)
startActivity(intent)
}
return super.onOptionsItemSelected(item)
}
}