簡體   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