A partir de um botão para cadastro do usuário, quero buscar uma imagem na galeria e inserir ela no imageview, já consigo fazer com uma foto tirada da camera na hora mas não da galeria, tentei todos MediaStore mas não consegui.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_cad_img_aparelho)
imageProfile = findViewById(R.id.iv_perfil_img)
val fotocamera = findViewById<View>(R.id.bt_tirar_foto) as Button
fotocamera.setOnClickListener{ takePicture() }
var profileImage = PreferenceManager.getDefaultSharedPreferences(this).getString(MediaStore.EXTRA_OUTPUT, null)
if(profileImage != null){
photoURI = Uri.parse(profileImage)
imageProfile.setImageURI(photoURI)
}else{
imageProfile.setImageResource(R.drawable.rosto)
}
val pularetapa = findViewById<View>(R.id.bt_pular_etapa) as Button
pularetapa.setOnClickListener{
imagemvazia = true
val it = Intent(this, fimconfig::class.java)
startActivity(it)
}
val voltarAparelho = findViewById<View>(R.id.iv_voltariniconfig3) as ImageButton
voltarAparelho.setOnClickListener{
val it = Intent(this, cad_nome_aparelho::class.java)
startActivity(it)
}
val cadimg = findViewById<View>(R.id.bt_salvar_img_aparelho) as Button
cadimg.setOnClickListener{
val it = Intent(this, fimconfig::class.java)
startActivity(it)
}
}
@Throws(IOException::class)
private fun createImageFile(): File{
val timesTamp: String = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())
val storageDir: File? = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
return File.createTempFile(
"PROFILE ${timesTamp}",".jpg",storageDir
)
}
private fun takePicture (){
Intent(MediaStore.ACTION_IMAGE_CAPTURE).also{ intent ->
intent.resolveActivity(packageManager)?.also{
val photoFile: File? = try{
createImageFile()
}catch (ex: IOException){
null
}
photoFile?.also{
photoURI = FileProvider.getUriForFile(this, "com.example.procartao.fileprovider",it)
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
startActivityForResult(intent, REQUEST_TAKE_PHOTO)
}
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?){
super.onActivityResult(requestCode,resultCode,data)
PreferenceManager.getDefaultSharedPreferences(this).apply{
edit().putString(MediaStore.EXTRA_OUTPUT, photoURI.toString()).apply()
}
imageProfile.setImageURI(photoURI)
}
}