目前最新版本(官网最新43903版本SDK)仅支持本地文件查看
测试Demo:TBS腾讯浏览器服务x5内核打开本地doc(docdocxxlsxpdfppt)文档(官网最新43903版本SDK)-互联网文档类资源-CSDN下载
腾讯TBS初始化失败,加载失败问题 : https://blog.csdn.net/jifenglie/article/details/106669723
配置:
1、gradle中引入(使用此方法,不需要在去官网下载SDK,也不需要下载SO文件)
implementation 'com.tencent.tbs.tbssdk:sdk:43903'
2、在gradle中配置 ndk
//X5兼容64位手机
ndk {
abiFilters "armeabi", "armeabi-v7a", "x86", "mips"
}
3、在你的Application 的onCreate 方法中初始化:
//初始化X5内核
QbSdk.initX5Environment(this, new QbSdk.PreInitCallback() {
@Override
public void onCoreInitFinished() {
//x5内核初始化完成回调接口,此接口回调并表示已经加载起来了x5,有可能特殊情况下x5内核加载失败,切换到系统内核。
}
@Override
public void onViewInitFinished(boolean b) {
//x5內核初始化完成的回调,为true表示x5内核加载成功,否则表示x5内核加载失败,会自动切换到系统内核。
Log.e("@@","加载内核是否成功:"+b);
}
});
使用:
1、本地文件存在,直接使用TbsReaderView打开:
private String tbsReaderTemp = Environment.getExternalStorageDirectory() + "/TbsReaderTemp";
private void displayFile(String filePath, String fileName) {
//增加下面一句解决没有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失败!!!!!");
}
}
Bundle bundle = new Bundle();
Log.d("print", "filePath" + filePath);//可能是路径错误
Log.d("print", "tempPath" + tbsReaderTemp);
bundle.putString("filePath", filePath);
bundle.putString("tempPath", tbsReaderTemp);
boolean result = mTbsReaderView.preOpen(getFileType(fileName), false);
Log.d("print", "查看文档---" + result);
if (result) {
mTbsReaderView.openFile(bundle);
} else {
}
}
2、文件不存在去下载
OkGo.get(docUrl)//
.tag(this)//
.execute(new FileCallback(download, docName) { //文件下载时,可以指定下载的文件目录download和文件名docName
@Override
public void onSuccess(File file, Call call, Response response) {
// file 即为文件数据,文件保存在指定目录
Log.d("print", "下载文件成功");
displayFile(download + file.getName(), file.getName());
Log.d("print", "" + file.getName());
}
@Override
public void downloadProgress(long currentSize, long totalSize, float progress, long networkSpeed) {
//这里回调下载进度(该回调在主线程,可以直接更新ui)
Log.d("print", "总大小---" + totalSize + "---文件下载进度---" + progress);
}
});
完整的展示pdf文件的代码:
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.widget.RelativeLayout;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;
import com.tencent.smtt.sdk.TbsReaderView;
import java.io.File;
import okhttp3.Call;
import okhttp3.Response;
public class TbsActivity extends AppCompatActivity implements TbsReaderView.ReaderCallback {
RelativeLayout mRelativeLayout;
private TbsReaderView mTbsReaderView;
// private String docUrl = "/test\\/WD802-8.doc";
// private String docUrl = "/test\\/安卓类库整理20190125.xlsx";
// private String docUrl = "/test\\/登录.pdf";
private String docUrl = "/test\\/新个人所得税法扣缴及六项专项附加扣除操作指引.ppt";
// private String docUrl = "/test\\/接口需求0327.docx";
// private String docUrl = "http:\\/\\/zyweike.cdn.bcebos.com\\/zyweike\\/ep1\\/2018\\/04\\/02\\/周末加班统计.xlsx";
private String download = Environment.getExternalStorageDirectory() + "/download/test/document/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tbs);
mTbsReaderView = new TbsReaderView(this, this);
mRelativeLayout = findViewById(R.id.tbsView);
mRelativeLayout.addView(mTbsReaderView, new RelativeLayout.LayoutParams(-1, -1));
Intent intent = getIntent();
String data = intent.getStringExtra("type");
if ("1".equals(data)) {
this.docUrl = "/test\\\\/WD802-8.doc";
} else if ("2".equals(data)) {
docUrl = "/test\\\\/接口需求0327.docx";
}else if("3".equals(data)){
docUrl = "/test\\\\/登录.pdf";
}else if("4".equals(data)){
docUrl = "/test\\\\/安卓类库整理20190125.xlsx";
} else if ("5".equals(data)) {
docUrl = "/test\\/新个人所得税法扣缴及六项专项附加扣除操作指引.ppt";
}
initDoc();
}
private void initDoc() {
int i = docUrl.lastIndexOf("/");
String docName = docUrl.substring(i, docUrl.length());
Log.d("print", "---substring---" + docName);
// String[] split = docUrl.split("\\/");
// String s = split[split.length - 4] + split[split.length - 3] + split[split.length - 2] + split[split.length - 1];
// Log.d("print", "截取带时间---" + s);
//判断是否在本地/[下载/直接打开]
File docFile = new File(download, docName);
if (docFile.exists()) {
//存在本地;
Log.d("print", "本地存在");
displayFile(docFile.toString(), docName);
} else {
OkGo.get(docUrl)//
.tag(this)//
.execute(new FileCallback(download, docName) { //文件下载时,可以指定下载的文件目录和文件名
@Override
public void onSuccess(File file, Call call, Response response) {
// file 即为文件数据,文件保存在指定目录
Log.d("print", "下载文件成功");
displayFile(download + file.getName(), file.getName());
Log.d("print", "" + file.getName());
}
@Override
public void downloadProgress(long currentSize, long totalSize, float progress, long networkSpeed) {
//这里回调下载进度(该回调在主线程,可以直接更新ui)
Log.d("print", "总大小---" + totalSize + "---文件下载进度---" + progress);
}
});
}
}
private String tbsReaderTemp = Environment.getExternalStorageDirectory() + "/TbsReaderTemp";
private void displayFile(String filePath, String fileName) {
//增加下面一句解决没有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失败!!!!!");
}
}
Bundle bundle = new Bundle();
/* 1.TbsReader: Set reader view exception:Cannot add a null child view to a ViewGroup
TbsReaderView: OpenFile failed! [可能是文件的路径错误]*/
/* 2.插件加载失败
so文件不支持;*/
/*
ndk {
//设置支持的SO库架构 'arm64-v8a',
abiFilters 'armeabi', "armeabi-v7a", 'x86'
} */
/*
3.自适应大小
*/
Log.d("print", "filePath" + filePath);//可能是路径错误
Log.d("print", "tempPath" + tbsReaderTemp);
bundle.putString("filePath", filePath);
bundle.putString("tempPath", tbsReaderTemp);
boolean result = mTbsReaderView.preOpen(getFileType(fileName), 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) {
}
@Override
protected void onDestroy() {
super.onDestroy();
mTbsReaderView.onStop();
}
}
activity_tbs.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/tbsView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
测试Demo:TBS腾讯浏览器服务x5内核打开本地doc(docdocxxlsxpdfppt)文档(官网最新43903版本SDK)-互联网文档类资源-CSDN下载
腾讯TBS初始化失败,加载失败问题 : https://blog.csdn.net/jifenglie/article/details/106669723