Vamos deixar que o Android gerencie e mostre os arquivos usando o próprio gerenciador dele.
Abaixo vou mostrar como fazer para recuperar, audios, vídeos, imagens e arquivos em geral.
fun buscarAudio,() {
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "audio/*"
intent.addCategory(Intent.CATEGORY_OPENABLE)
try {
startActivityForResult(Intent.createChooser(intent, "Selecione o anexo"), REQUEST_CODE_AUDIO) //O REQUEST_CODE_AUDIO é um código INT que o desenvolvedor escolhe para mapear o retorno.
} catch (e: ActivityNotFoundException) {
Toast.makeText(this, "Não encontramos nenhum gerenciador de arquivos",
Toast.LENGTH_SHORT).show()
}
}
fun buscarVideo,() {
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "video/*"
intent.addCategory(Intent.CATEGORY_OPENABLE)
try {
startActivityForResult(Intent.createChooser(intent, "Selecione o anexo"), REQUEST_CODE_VIDEO) //O REQUEST_CODE_VIDEO é um código INT que o desenvolvedor escolhe para mapear o retorno.
} catch (e: ActivityNotFoundException) {
Toast.makeText(this, "Não encontramos nenhum gerenciador de arquivos",
Toast.LENGTH_SHORT).show()
}
}
fun buscarImage,() {
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "image/*"
intent.addCategory(Intent.CATEGORY_OPENABLE)
try {
startActivityForResult(Intent.createChooser(intent, "Selecione o anexo"), REQUEST_CODE_IMAGEM) //O REQUEST_CODE_IMAGEM é um código INT que o desenvolvedor escolhe para mapear o retorno.
} catch (e: ActivityNotFoundException) {
Toast.makeText(this, "Não encontramos nenhum gerenciador de arquivos",
Toast.LENGTH_SHORT).show()
}
}
fun buscarArquivo() {
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "*/*"
intent.addCategory(Intent.CATEGORY_OPENABLE)
try {
startActivityForResult(Intent.createChooser(intent, "Selecione o anexo"), REQUEST_CODE_FILE) //O REQUEST_CODE_FILE é um código INT que o desenvolvedor escolhe para mapear o retorno.
} catch (e: ActivityNotFoundException) {
Toast.makeText(this, "Não encontramos nenhum gerenciador de arquivos",
Toast.LENGTH_SHORT).show()
}
}
Após termos recuperado qualquer arquivo, precisamos poder utilizar eles, agora vou mostrar como fazer isso.
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (REQUEST_CODE_AUDIO == requestCode && Activity.RESULT_OK == resultCode && data != null)
{
try {
val uri = data.data ?: throw IllegalAccessException()
//utilizar o arquivo
} catch (th: Throwable) {
//tratar erro ao recuperar a URI
}
}
else if (REQUEST_CODE_VIDEO == requestCode && Activity.RESULT_OK == resultCode && data != null)
{
try {
val uri = data.data ?: throw IllegalAccessException()
//utilizar o arquivo
} catch (th: Throwable) {
//tratar erro ao recuperar a URI
}
}
else if (REQUEST_CODE_IMAGEM == requestCode && Activity.RESULT_OK == resultCode && data != null)
{
try {
val uri = data.data ?: throw IllegalAccessException()
//utilizar o arquivo
} catch (th: Throwable) {
//tratar erro ao recuperar a URI
}
}
else if (REQUEST_CODE_FILE == requestCode && Activity.RESULT_OK == resultCode && data != null)
{
try {
val uri = data.data ?: throw IllegalAccessException()
//utilizar o arquivo
} catch (th: Throwable) {
//tratar erro ao recuperar a URI
}
}
}
0 comentários