其實(shí)大家都是下載文件后加載出來,Android機(jī)如果沒有對應(yīng)的文件處理應(yīng)用,那么是不能查看文件的,所以我們需要幫助。
目前選擇了迅騰的TBS,如何接入見官網(wǎng)文檔。
注意一點(diǎn)就好了,SDK選擇這個(gè):

1538200509(1).png
這個(gè)版本有
QbSdk.openFileReader(ctx,”/sdcard/xxx.doc”, params,callback);
這個(gè)類可以使用,不過我目前也沒有用 = =|||
假設(shè)我們已經(jīng)下載好了文件,那么預(yù)覽文件的代碼如下:
tbsReaderView = new TbsReaderView(this, readerCallback);
mViewParent.addView(tbsReaderView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
openFile(path);
private void openFile(String path) {
Bundle bundle = new Bundle();
//文件路徑
bundle.putString("filePath", path);
//臨時(shí)文件的路徑,必須設(shè)置,否則會報(bào)錯(cuò)
String tempPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/TBS";
File tempFile = new File(tempPath);
if (!tempFile.exists()) {
tempFile.mkdir();
}
bundle.putString("tempPath", tempPath + "/TBS");
//準(zhǔn)備
boolean result = tbsReaderView.preOpen(path.substring(path.lastIndexOf('.') + 1), false);
if (result) {
//預(yù)覽文件
tbsReaderView.openFile(bundle);
}
}
preOpen的第一個(gè)參數(shù)是文件類型,這里并不是MIME類型,而是doc、docx之流。