繁体   English   中英

如何从内部缓存目录 jetpack compose 打开 pdf 文件

[英]how to open pdf file from internal cache dir jetpack compose

我正在尝试从内部缓存目录打开保存的 pdf 文件。 以“旧”方式,我的代码看起来像这样。

在jetpack compose中,我可以使用这部分代码,正在创建pdf,我可以在设备资源管理器中看到它。 但是我怎样才能在屏幕上显示 pdf 呢?

    fun decodeTestPdfString(pdf_string:String, context:Context) {

        //make FileOutPutStream
        var fos: FileOutputStream? = null

        try {
            if (pdf_string != null) {
                f = File(context?.cacheDir, "testFile" + ".pdf")
                f!!.createNewFile()

                fos = FileOutputStream(f)
                val decodedString: ByteArray = Base64.decode(pdf_string, Base64.DEFAULT)
                fos.write(decodedString)
                fos.flush()
                fos.close()


            }
        } catch (e: Exception) {
        } finally {
            if (fos != null) {
                fos = null
            }
        }



        val path: Uri = FileProvider.getUriForFile(context!!, context!!.getApplicationContext().getPackageName() + ".provider", f!!)
        val intent = Intent(Intent.ACTION_VIEW)
        intent.setDataAndType(path, "application/pdf")
        intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)



        try {
            startActivity(intent)
        } catch (e: ActivityNotFoundException) {

        }


    }
}

但是最后一步(尝试意图)是提出问题。 该文件是在缓存目录中创建的,我可以看到它。

要打开 pdf 阅读器:

startActivity(context, intent, options:null)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM