集成SDK参考:https://x5.tencent.com/docs/access.html
需求:实现本地文件的浏览,包括不限于PDF,WORD,EXECEL等等
1、引入依赖
implementation 'com.tencent.tbs:tbssdk:44165'
2、创建三个类
代码语言:javascript复制public class X5App {
public void initX5app(Context context){
//设置非wifi条件下允许下载X5内核
QbSdk.setDownloadWithoutWifi(true);
//搜集本地tbs内核信息并上报服务器,服务器返回结果决定使用哪个内核。
QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {
@Override
public void onViewInitFinished(boolean arg0) {
//x5內核初始化完成的回调,true表x5内核加载成功,否则表加载失败,会自动切换到系统内核。
Log.d("app", " 内核加载 " arg0);
}
@Override
public void onCoreInitFinished() {
}
};
//x5内核初始化接口
QbSdk.initX5Environment(context, cb);
}
}
代码语言:javascript复制public class X5WebView extends WebView {
private WebViewClient client = new WebViewClient() {
/**
* 防止加载网页时调起系统浏览器
*/
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
};
@SuppressLint("SetJavaScriptEnabled")
public X5WebView(Context arg0, AttributeSet arg1) {
super(arg0, arg1);
this.setWebViewClient(client);
initWebViewSettings();
this.getView().setClickable(true);
}
private void initWebViewSettings() {
WebSettings mWebSettings = this.getSettings();
mWebSettings.setJavaScriptEnabled(true);
mWebSettings.setJavaScriptCanOpenWindowsAutomatically(true);
mWebSettings.setAllowFileAccess(true);
mWebSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
mWebSettings.setSupportZoom(true);
mWebSettings.setBuiltInZoomControls(true);
mWebSettings.setUseWideViewPort(true);
mWebSettings.setSupportMultipleWindows(true);
mWebSettings.setAppCacheEnabled(true);
mWebSettings.setDomStorageEnabled(true);
mWebSettings.setGeolocationEnabled(true);
mWebSettings.setAppCacheMaxSize(Long.MAX_VALUE);
mWebSettings.setPluginState(WebSettings.PluginState.ON_DEMAND);
mWebSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
}
public X5WebView(Context arg0) {
super(arg0);
setBackgroundColor(85621);
}
}
代码语言:javascript复制public class X5TbsFileServicePage extends AppCompatActivity implements TbsReaderView.ReaderCallback {
private String tbsReaderTemp = Environment.getExternalStorageDirectory() "/TbsReaderTemp";
TbsReaderView mTbsReaderView;
RelativeLayout mRelativeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_x5_tbs_file_service_page);
// 绑定容器
mRelativeLayout = findViewById(R.id.X5TbsView);
// FileModel fileModel = new FileModel();
// fileModel.setPath("/storage/emulated/0/demo/word.docx");
// displayFile(fileModel);
}
public void displayFile(FileModel fileModel) {
mTbsReaderView = new TbsReaderView(this,this);
//增加下面一句解决没有TbsReaderTemp文件夹存在导致加载文件失败
String bsReaderTemp = tbsReaderTemp;
File bsReaderTempFile =new File(bsReaderTemp);
if (!bsReaderTempFile.exists()) {
Log.d("print","文件不存在准备创建/TbsReaderTemp!!");
boolean mkdir = bsReaderTempFile.mkdir();
if(!mkdir){
Log.d("print","创建/TbsReaderTemp失败!!!!!");
}
Toast.makeText(this,"文件不存在!",Toast.LENGTH_SHORT).show();
}
mRelativeLayout.addView(mTbsReaderView,new RelativeLayout.LayoutParams(-1,-1));
Bundle bundle = new Bundle();
bundle.putString("filePath",fileModel.getPath());
bundle.putString("tempPath", tbsReaderTemp);
boolean result = mTbsReaderView.preOpen(getFileType("word.docx"), false);
Log.d("print","查看文档---" result);
if (result) {
mTbsReaderView.openFile(bundle);
}else{
}
}
private String getFileType(String paramString) {
String str = "";
if (TextUtils.isEmpty(paramString)) {
Log.d("print", "paramString---->null");
return str;
}
Log.d("print", "paramString:" paramString);
int i = paramString.lastIndexOf('.');
if (i <= -1) {
Log.d("print", "i <= -1");
return str;
}
str = paramString.substring(i 1);
Log.d("print", "paramString.substring(i 1)------>" str);
return str;
}
@Override
public void onCallBackAction(Integer integer, Object o, Object o1) {
Log.i("触摸监听:"," ");
}
/**
* 返回上个页面
* @param keyCode
* @param event
* @return
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return super.onKeyDown(keyCode, event);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mTbsReaderView != null) {
mTbsReaderView.onStop();
mTbsReaderView.destroyDrawingCache();
}
}
}
3、在Application中的OnCreate方法中进行初始化
代码语言:javascript复制new X5App().initX5app(mContext);
4、使用
通过点击事件触发预览 Kotlin版
代码语言:javascript复制 //预览事件
stvOpen.setOnClickListener(View.OnClickListener {
val extraParams = HashMap<String, String>()
extraParams["style"] = "1"
extraParams["local"] = "true"
QbSdk.openFileReader(this, fileName?.absolutePath, extraParams, object : ValueCallback<String?> {
override fun onReceiveValue(s: String?) {
Log.e("TAG", "OpenFile Callback: $s")
if ("openFileReader open in QB" == s || "filepath error" == s || "TbsReaderDialogClosed" == s || "fileReaderClosed" == s) {
QbSdk.closeFileReader(me)
}
}
})
})