什么是TBS
依托 X5 内核强大的能力,致力于提供优化移动端浏览体验的整套解决方案(官网介绍)。我们可利用其做文件浏览功能,支持多种文件格式,完全可以满足需求。更多介绍请看官网:http://x5.tencent.com/
接入TBS
TBS只能浏览本地文件,对于远程文件需要先进行下载,我们先看浏览本地文件如何实现。
一、 下载SDK:http://x5.tencent.com/tbs/sdk.html,将示例中lib下的jar文件 和 jniLibs下的so文件 copy至自己的项目中。

如果so文件是放置于 libs 目录下,需要在 app/build.gradle中指定路径:
android {
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
}
二、 添加权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION"/>
<uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
注:某些机型可能预览失败, 可能是so文件无法适应64位机型,需在build.gradle下的defaultConfig{}中加入:
ndk {
abiFilters "armeabi", "armeabi-v7a", "x86", "mips"
}
三、使用
1.在application类中初始化tbs内核
public class MyApplication extends LitePalApplication {
private static Context mContext;
public static Context getInstance() {
return mContext;
}
@Override
protected void attachBaseContext(Context context) {
super.attachBaseContext(context);
MultiDex.install(context);
}
@Override
public void onCreate() {
super.onCreate();
mContext = this;
//初始化X5内核
QbSdk.initX5Environment(this, new QbSdk.PreInitCallback() {
@Override
public void onCoreInitFinished() {
//x5内核初始化完成回调接口,此接口回调并表示已经加载起来了x5,有可能特殊情况下x5内核加载失败,切换到系统内核。
Log.e("@@", "加载内核是否onCoreInitFinished成功:");
}
@Override
public void onViewInitFinished(boolean b) {
//x5內核初始化完成的回调,为true表示x5内核加载成功,否则表示x5内核加载失败,会自动切换到系统内核。
Log.e("@@", "加载内核是否成功:" + b);
}
});
}
}
2.在要显示文档效果的activity类中:
/**
* 展示PDF文件Activity
*/
public class PdfActivity extends BaseActivity implements TbsReaderView.ReaderCallback {
String pdfName;
private String pdfName1;
private String pdfUrl;
private TbsReaderView mTbsReaderView;
@Override
protected int getLayoutID() {
return R.layout.activity_show_pdf;
}
@Override
public void initView() {
Bundle extras = getIntent().getExtras();
pdfName1 = (String) extras.get("pdfName");
pdfUrl = (String) extras.get("pdfUrl");
pdfName = Environment.getExternalStorageDirectory() +
"/com.zftcm.patient/" + pdfName1;
display(this.pdfName);
mTbsReaderView = new TbsReaderView(this, this);
RelativeLayout rootRl = (RelativeLayout) findViewById(R.id.tb);
rootRl.addView(mTbsReaderView, new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
Bundle bundle = new Bundle();
bundle.putString("filePath", pdfName);
bundle.putString("tempPath", Environment.getExternalStorageDirectory().getPath());
File file = new File(pdfName);
boolean result = mTbsReaderView.preOpen(parseFormat(pdfName), false);
if (result) {
mTbsReaderView.openFile(bundle);
}
}
private String parseFormat(String fileName) {
return fileName.substring(fileName.lastIndexOf(".") + 1);
}
private void display(String assetFileName) {
File file = new File(assetFileName, pdfName1);
}
@Override
public void initPresenter() {
}
@Override
public void onCallBackAction(Integer integer, Object o, Object o1) {
}
@Override
protected void onDestroy() {
super.onDestroy();
mTbsReaderView.onStop();
}
}
1590





