1

hi i want open excel files from my app in another office app i used this codes

val file = File(path)
        val uri = FileProvider.getUriForFile(
            activity!!.applicationContext,
            BuildConfig.APPLICATION_ID.toString() + ".provider",
            file
        )
        val intent = Intent(Intent.ACTION_VIEW)

        //file is word
        if (file.extension.contains("doc")){
            intent.setDataAndType(uri, "application/word")

        }else{
            // file is excel
            intent.setDataAndType(uri, "application/excel")
        }
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

        try {
            activity.startActivity(intent)
        } catch (e: ActivityNotFoundException) {
            snack(view,"برنامه ای برای نمایش فایل ورد/اکسل روی گوشی پیدا نشد، لطفا برنامه آفیس را روی گوشی خود نصب کنید")
        }

but this code not work properly and it doesn't work on some phones and Android 10 So how do I make it work on all phones?

davod kh
  • 23
  • 4
  • Does this help? https://android.jlelse.eu/handling-files-in-code-after-the-android-10-released-2bea0e16d35 – Animesh Sahu May 12 '20 at 15:36
  • Does this answer your question? [Can't create directory in Android 10](https://stackoverflow.com/questions/58379543/cant-create-directory-in-android-10) – Animesh Sahu May 12 '20 at 15:37
  • "this code not work properly and it doesn't work on some phones and Android 10" isn't descriptive enough to help people understand your problem. Instead, describe what the exact observed behavior is and what the expected/intended behavior should be. For UI issues, a screenshot or video is usually helpful. Include the exact text of any error messages, including the full [stack trace](/a/23353174) of any exceptions, if applicable, as well as which line of code the stack trace points to. Please see [ask] and [How to create a Minimal, Reproducible Example](/help/minimal-reproducible-example). – Ryan M May 14 '20 at 03:14

1 Answers1

0

I finally found a way myself. I post it here, maybe it helped someone

val file = File(path)

        val myMime: MimeTypeMap = MimeTypeMap.getSingleton()
        val newIntent = Intent(Intent.ACTION_VIEW)

        val mimeType: String =
            myMime.getMimeTypeFromExtension(file.extension).toString()
        if (Build.VERSION.SDK_INT >= 24) {
            newIntent.setDataAndType(FileProvider.getUriForFile(activity!!.applicationContext, activity.applicationContext.packageName.toString() +
                ".provider", file), mimeType)
        }else{
            newIntent.setDataAndType(Uri.fromFile(file), mimeType)
        }
        newIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        newIntent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
        try {
            activity!!.startActivity(newIntent)
        } catch (e: ActivityNotFoundException) {
            snack(view,"برنامه ای برای نمایش فایل ورد/اکسل روی گوشی پیدا نشد، لطفا برنامه آفیس را روی گوشی خود نصب کنید")
        }

also you need to do this

davod kh
  • 23
  • 4