I'm coding an instagram clone and i must request external storage permission but my app doesn't respond when i clicked imageView that ı set onClick method (fotoSec) for taking image from gallery.
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)
}
And there is no exceptions in logcat. In addition I'm a beginner, sorry my faults.
Thanks in advance.
CodePudding user response:
make sure that you have added the same permission in AndroidManifest.xml like below.