I am making an API call to get a file to download and open (with kotlin).
The response I get through Retrofit and a ResponseBody.
Until the call is ok, I get the result.
Could you help me to save the file and especially to open it autametically. When I make the call, I know the name and extension of the file, so I know if it is a jpg or a pdf or an XLS.
Is there a way for the system to automatically open the files according to the extension or do I have to show the window to choose the application ( in this case how do I show it)?
UPDATE
I managed to understand how to download the file and how to open the files. But I cannot do this for all files, when I open the new view if the phone does not have the application, nothing happens. So I ask how can I download the file and show the notification that the file has been downloaded? I added file.writeBytes but the phone doesn't give me any notification like when downloading a file from an email or browser.
if I understand correctly when I do not have the application installed it goes on the catch
override fun onSuccess(result: Response<ResponseBody>) {
var d = result
try {
file = File(
requireContext().filesDir.path.toString() + "/" + idEmail +"_" + intitule.replace(
"/",
"_"
) + extensionFichier
)
file.writeBytes(result.body()!!.bytes())
val intent = Intent(Intent.ACTION_VIEW)
val uri = FileProvider.getUriForFile(
requireContext(),
BuildConfig.APPLICATION_ID + ".provider",
file
)
val type = when(extensionFichier.lowercase()){
// Image file
".png",".jpg",".jpeg",".gif" -> "image/*"
// PDF file
".pdf" -> "application/pdf"
// Excel file
".xls" -> "application/ms-excel"
// Word document
".doc",".docx" -> "application/msword"
// Powerpoint file
".ppt",".pptx" -> "application/ms-powerpoint"
// Zip file
".zip" -> "application/zip"
// Rar file
".rar" -> "application/rar"
// Audio file
".mp3",".m4a",".wav" -> "audio/*"
// Video file
".3gp",".mpg",".mpeg",".mpe",".mp4",".avi" -> "audio/*"
// Txt file
".txt" -> "image/jpeg"
else -> "*/*\""
}
intent.setDataAndType(uri,type)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
requireContext().startActivity(intent)
}
catch (e: Exception) {
println(e.message)
}
finally, I cannot manage the back in the new view has opened
Thanks