How to show all types of documents (.docx, .csv, .xls, .pdf, .txt) stored in s3 in android application?

Viewed 252

I have multiple type of file stored in s3 e.g; .docx, .txt, .csv, .xls, etc. I want to display all these documents in my android application. Is there any common way to display all types of documents in android? (Like in iOS we can use WKWebView to display all type of files)

I have tried WebView. Its only displaying .docx, .txt and .pdf files. But I want to display .xls and .csv too.

1 Answers

I know this may not be the best solution but, I would recommend you to download the file temporarily and open it via intent because it is not possible that you can open all type of file in webview.

You can write a utility class to download the file and then pass it to intent and delete after the task is done.

private open inner class DownloadFile : AsyncTask<String, Void, String>() {

        override fun onPreExecute() {
            super.onPreExecute()
            AppUtil.showDialog(context)
        }

        override fun doInBackground(vararg strings: String): String? {

            var fName = strings[0].substring(strings[0].lastIndexOf('/') + 1)
            fName = fName.replace(" ", "_")


            val fileUrl = strings[0]
            val extStorageDirectory = Environment.getExternalStorageDirectory().toString()
            val folder = File(extStorageDirectory, "My_Application_name")
            folder.mkdir()

            val mFile = File(folder, fName)

            try {
                mFile.createNewFile()
            } catch (e: IOException) {
                e.printStackTrace()
            }

            FileDownloader.downloadFile(fileUrl, mFile)
            return fName
        }

        override fun onPostExecute(result: String?) {
            super.onPostExecute(result)
            AppUtil.dismissDialog()
            openMyFile(result)
        }
    }

Then use following intent to open that file

fun openFile(url: File) {

    val uri = Uri.fromFile(url)

    val intent = Intent(Intent.ACTION_VIEW)
    if (url.toString().contains(".doc") || url.toString().contains(".docx")) {
        // Word document
        intent.setDataAndType(uri, "application/msword")
    } else if (url.toString().contains(".pdf")) {
        // PDF file
        intent.setDataAndType(uri, "application/pdf")
    } else if (url.toString().contains(".ppt") || url.toString().contains(".pptx")) {
        // Powerpoint file
        intent.setDataAndType(uri, "application/vnd.ms-powerpoint")
    } else if (url.toString().contains(".xls") || url.toString().contains(".xlsx")) {
        // Excel file
        intent.setDataAndType(uri, "application/vnd.ms-excel")
    } else if (url.toString().contains(".zip") || url.toString().contains(".rar")) {
        // WAV audio file
        intent.setDataAndType(uri, "application/x-wav")
    } else if (url.toString().contains(".rtf")) {
        // RTF file
        intent.setDataAndType(uri, "application/rtf")
    } else if (url.toString().contains(".wav") || url.toString().contains(".mp3")) {
        // WAV audio file
        intent.setDataAndType(uri, "audio/x-wav")
    } else if (url.toString().contains(".gif")) {
        // GIF file
        intent.setDataAndType(uri, "image/gif")
    } else if (url.toString().contains(".jpg") || url.toString().contains(".jpeg") || url.toString().contains(".png")) {
        // JPG file
        intent.setDataAndType(uri, "image/jpeg")
    } else if (url.toString().contains(".txt")) {
        // Text file
        intent.setDataAndType(uri, "text/plain")
    } else if (url.toString().contains(".3gp") || url.toString().contains(".mpg") || url.toString().contains(".mpeg") || url.toString().contains(
            ".mpe"
        ) || url.toString().contains(".mp4") || url.toString().contains(".avi")
    ) {
        // Video files
        intent.setDataAndType(uri, "video/*")
    } else {
        //if you want you can also define the intent type for any other file
        //additionally use else clause below, to manage other unknown extensions
        //in this case, Android will show all applications installed on the device
        //so you can choose which application to use
        intent.setDataAndType(uri, "*/*")
    }

    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    startActivity(intent)

}

Once your task is over you can delete the downloaded file.

Related