android - 文件路径的位图 decodefile() 显示为空
问题描述
我是使用 Kotlin 进行 Android 开发的新手。我正在尝试使用以下代码中的一些方法将图像从手机上传到我的应用程序。在某些过程中一切正常,例如我可以在 imageview 上选择然后屏幕弹出窗口可以让我在手机中选择图像。不幸的是,选择图像后,它显示为空/空白,在应用程序中不显示任何内容。我试图寻求帮助,但没有成功。希望我能在这里得到帮助。提前致谢!
活动代码
package student
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.BitmapFactory
import android.os.Build
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.example.amazontutoringcenter.databinding.ActivityMainBinding
import com.example.amazontutoringcenter.databinding.ActivityStudentClassroomBinding
import com.squareup.picasso.Picasso
class StudentClassroomActivity: AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
binding.imageView.setOnClickListener {
checkPermission()
}
}
val READ_IMAGE:Int=338
private fun checkPermission(){
if(Build.VERSION.SDK_INT>=23){
if(ActivityCompat.checkSelfPermission(this,
android.Manifest.permission.READ_EXTERNAL_STORAGE)!=
PackageManager.PERMISSION_GRANTED){
requestPermissions(arrayOf( android.Manifest.permission.READ_EXTERNAL_STORAGE),READ_IMAGE)
return
}
}
loadImage()
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
when(requestCode){
READ_IMAGE->{
if(grantResults[0]==PackageManager.PERMISSION_GRANTED){
loadImage()
}else{
Toast.makeText(applicationContext,"Cannot access your images",Toast.LENGTH_LONG).show()
}
}
else-> super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
}
val PICK_IMAGE_CODE=123
private fun loadImage(){
var intent=Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(intent,PICK_IMAGE_CODE)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(requestCode==PICK_IMAGE_CODE && data!=null && resultCode == RESULT_OK){
val selectedImage=data.data
val filePathColum= arrayOf(MediaStore.Images.Media.DATA)
val cursor= contentResolver.query(selectedImage!!,filePathColum,null,null,null)
cursor!!.moveToFirst()
val coulomIndex=cursor!!.getColumnIndex(filePathColum[0])
val picturePath=cursor!!.getString(coulomIndex)
cursor!!.close()
//.load(picturePath).into(binding.imageStudentProfile)
//Picasso.get().load(picturePath).fit().into(binding.imageStudentProfile)
binding.imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath))
}
}
}
XML 文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".MainActivity">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test Layout"
android:layout_marginTop="50dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2"
android:src="@drawable/ic_attendant"/>
</androidx.constraintlayout.widget.ConstraintLayout>
解决方案
ImageView 有一个成员可以从 uri 加载图像。
你有一个 uri data.data
。
所以使用它。
推荐阅读
- android - 无法从 afw#setup 访问 Android Enterprise
- python - Python tar 文件模块显示两个具有相同名称的文件,除了一个以 _ 开头
- r - 如何根据 webscrape 中的表设置列名?
- android - android系统的块设备和挂载点有什么区别
- javascript - useHistory可以作为useRef的值吗?
- css - 我可以根据章节或小节而不是章节来编号定理吗?
- python - 如何优化 DataFrame 的分组并对组执行操作
- grunt-connect-proxy - grunt-connect-proxy 配置不起作用;给出“验证属性 connect.server 是否存在于配置中...错误”
- javascript - 状态改变导致 APP 组件重新渲染
- asp.net - 剃须刀页面 asp-page 不呈现正确的 html