0

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

Kevin
  • 415
  • 3
  • 8
  • 21
  • `system to automatically open` luckily no. Also please include a relevant code – Marcin Orlowski Apr 24 '23 at 09:22
  • Does this answer your question? [How to open a file in Android via an Intent](https://stackoverflow.com/questions/12585747/how-to-open-a-file-in-android-via-an-intent) – m0skit0 Apr 24 '23 at 09:36
  • For security reasons, it's the user that must choose which app to open a file with. You can just send an intent to the system asking it to open a file. I will assume you're talking about Android. Next time please add relevant tags like Android. – m0skit0 Apr 24 '23 at 09:36
  • @MarcinOrlowski I don't have any code that works yet, I only have the result of the API call which is a ResponseBody, and I don't know how to go on to show the file – Kevin Apr 24 '23 at 09:38
  • @m0skit0 I added the tag and wrote Kotlin, I thought that was enough. – Kevin Apr 24 '23 at 09:38
  • Kotlin can be used to develop in any system, not just Android. – m0skit0 Apr 24 '23 at 09:39
  • @MarcinOrlowski I added the only code I tested, from some examples but that doesn't work – Kevin Apr 24 '23 at 09:47

0 Answers0