其实大家都是下载文件后加载出来,Android机如果没有对应的文件处理应用,那么是不能查看文件的,所以我们需要帮助。
目前选择了迅腾的TBS,如何接入见官网文档。
注意一点就好了,SDK选择这个:
1538200509(1).png
这个版本有
QbSdk.openFileReader(ctx,”/sdcard/xxx.doc”, params,callback);
这个类可以使用,不过我目前也没有用 = =|||
假设我们已经下载好了文件,那么预览文件的代码如下:
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);
//临时文件的路径,必须设置,否则会报错
String tempPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/TBS";
File tempFile = new File(tempPath);
if (!tempFile.exists()) {
tempFile.mkdir();
}
bundle.putString("tempPath", tempPath + "/TBS");
//准备
boolean result = tbsReaderView.preOpen(path.substring(path.lastIndexOf('.') + 1), false);
if (result) {
//预览文件
tbsReaderView.openFile(bundle);
}
}
preOpen的第一个参数是文件类型,这里并不是MIME类型,而是doc、docx之流。