目錄

前言
有的時候需要我們查看Office文件,像ppt、world等文件,而如果使用手機中存在的軟件打開的話(像wps等),又可能會因為手機中沒有相關(guān)的軟件而打不開,所以最好的方式就是在APP內(nèi)部打開
實現(xiàn)方法
1.使用騰訊TBS
2023年之前可能還行,但是現(xiàn)在騰訊已經(jīng)把文檔瀏覽服務(wù)分出去了,開始收費了
https://mp.weixin.qq.com/s/rmSa4Fs77MDdjFioRKwXPA
2.使用kkfileView
https://kkview.cn/zh-cn/index.html
這個是免費的,需要部署,然后就可以通過訪問網(wǎng)頁連接的形式來打開office文件了,當(dāng)然如果不想部署的話可以使用它提供的在線預(yù)覽的地址,它支持打開很多文件如下

在Android中使用的話分兩種情況,第一種情況是文件連接帶后綴名
val previewUrl = "http://127.0.0.1:8080/file/test.txt"
val url =
javaEncodeURIComponent(EncodeUtils.base64Encode2String(previewUrl.toByteArray()))
fun javaEncodeURIComponent(url: String?): String? {
return try {
URLEncoder.encode(url, "UTF-8").replace("+", "%20").replace("*", "%2A")
.replace("%7E", "~").replace("/", "%2F")
} catch (e: UnsupportedEncodingException) {
e.printStackTrace()
""
}
}
這里的EncodeUtils.base64Encode2String是使用的下面的依賴
implementation 'com.blankj:utilcodex:1.31.1'
第二種情況是文件連接不帶后綴名,要加一個參數(shù)fullfilename用來指定文件名
val fileUrl = "http://127.0.0.1:8080/filedownload"
val fileName = "test.txt"
val previewUrl = "$fileUrl?fullfilename=$fileName"
val url =
javaEncodeURIComponent(EncodeUtils.base64Encode2String(previewUrl.toByteArray()))
fun javaEncodeURIComponent(url: String?): String? {
return try {
URLEncoder.encode(url, "UTF-8").replace("+", "%20").replace("*", "%2A")
.replace("%7E", "~").replace("/", "%2F")
} catch (e: UnsupportedEncodingException) {
e.printStackTrace()
""
}
}