我的应用程序在应该请求许可时没有响应
my app is not responding when it should be asking for permission
我正在编写一个 instagram 克隆代码,我必须请求外部存储许可,但是当我单击 imageView 时我的应用程序没有响应,我设置了 onClick 方法 (fotoSec) 以从图库中获取图像。
fun fotoSec (view: View) {
// Önce izin istenecek. İzin verilirse galeriye gidecek verilmezse uyarı mesajı gösterecek.
// İzini bir kere verdiyse tekrar istenmeyecek direkt galeriye gidecek.
if (ContextCompat.checkSelfPermission(this,android.Manifest.permission.READ_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED) {
//İzin verilmemiş.
ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.READ_EXTERNAL_STORAGE),1)
} else {
//İzin verilmiş.
val galeriIntent = Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(galeriIntent,2)
}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
if (requestCode==1) {
if(grantResults.size>0 &&grantResults[0]== PackageManager.PERMISSION_GRANTED) {
val galeriIntent = Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(galeriIntent,2)
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if ( requestCode==2 && resultCode== Activity.RESULT_OK && data!=null) {
secilenGorsel=data.data
if (secilenGorsel!=null) {
if (Build.VERSION.SDK_INT>= 28) {
val source = ImageDecoder.createSource(this.contentResolver,secilenGorsel!!)
secilenBitmap=ImageDecoder.decodeBitmap(source)
image!!.setImageBitmap(secilenBitmap)
}else {
secilenBitmap= MediaStore.Images.Media.getBitmap(this.contentResolver,secilenGorsel)
image!!.setImageBitmap(secilenBitmap)
}
}
}
super.onActivityResult(requestCode, resultCode, data)
}
logcat也没有例外。
另外我是初学者,请见谅。
提前致谢。
确保您已在 AndroidManifest.xml 中添加相同的权限,如下所示。
我正在编写一个 instagram 克隆代码,我必须请求外部存储许可,但是当我单击 imageView 时我的应用程序没有响应,我设置了 onClick 方法 (fotoSec) 以从图库中获取图像。
fun fotoSec (view: View) {
// Önce izin istenecek. İzin verilirse galeriye gidecek verilmezse uyarı mesajı gösterecek.
// İzini bir kere verdiyse tekrar istenmeyecek direkt galeriye gidecek.
if (ContextCompat.checkSelfPermission(this,android.Manifest.permission.READ_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED) {
//İzin verilmemiş.
ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.READ_EXTERNAL_STORAGE),1)
} else {
//İzin verilmiş.
val galeriIntent = Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(galeriIntent,2)
}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
if (requestCode==1) {
if(grantResults.size>0 &&grantResults[0]== PackageManager.PERMISSION_GRANTED) {
val galeriIntent = Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(galeriIntent,2)
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if ( requestCode==2 && resultCode== Activity.RESULT_OK && data!=null) {
secilenGorsel=data.data
if (secilenGorsel!=null) {
if (Build.VERSION.SDK_INT>= 28) {
val source = ImageDecoder.createSource(this.contentResolver,secilenGorsel!!)
secilenBitmap=ImageDecoder.decodeBitmap(source)
image!!.setImageBitmap(secilenBitmap)
}else {
secilenBitmap= MediaStore.Images.Media.getBitmap(this.contentResolver,secilenGorsel)
image!!.setImageBitmap(secilenBitmap)
}
}
}
super.onActivityResult(requestCode, resultCode, data)
}
logcat也没有例外。 另外我是初学者,请见谅。
提前致谢。
确保您已在 AndroidManifest.xml 中添加相同的权限,如下所示。