赞
踩
记录一下安卓使用腾讯tbs浏览本地文档(word.pdf.ppt)踩过的坑,以方便以后自己面查阅跟供他人参考。
腾讯TBS服务官网介绍
遇到的坑:
1、按照文档接入sdk,并在DemoApplication 初始化加载tbs内核不成功,(周五、周六、周日)通常是初始化加载不成功,其他时间段没问题
2、tbs初始化加载成功,添加显示具体文档的时候,一直提示 "插件加载失败"
处理办法:
问题1:按照sdk流程初始化初始化加载tbs内核,加载内核不成功重新调用下载方法:
TbsDownloader.startDownload();
10次都加载不成功,直接手动加载安装本地tbs内核资源(本地tbs资源可以放在自己的服务器上):
QbSdk.installLocalTbsCore() 问题2: 根据官网常见问题做对应的修改即可: 腾讯浏览服务 3.7 targetAPI为Android P时无法下载内核?
下面附上代码段:
- package com.demo.tbsloadfile;
-
- import android.app.Application;
- import android.util.Log;
- import android.widget.Toast;
-
- import com.tencent.smtt.export.external.TbsCoreSettings;
- import com.tencent.smtt.sdk.QbSdk;
- import com.tencent.smtt.sdk.QbSdk.PreInitCallback;
- import com.tencent.smtt.sdk.TbsDownloader;
- import com.tencent.smtt.sdk.TbsListener;
-
- import java.io.File;
- import java.util.HashMap;
-
- public class DemoApplication extends Application {
-
- private static final String TAG = "DemoApplication";
-
- @Override
- public void onCreate() {
- super.onCreate();
-
- initTBS();
- //targetSdkVersion > 27 ,需要参考文档 3.7 targetAPI为Android P时无法下载内核?添加xml
- // https://x5.tencent.com/docs/questions.html
-
-
- //复制文件到sdkcard
- FileCopyAssetToSD.getInstance(getApplicationContext()).copyAssetsToSD("sources" , FileCopyAssetToSD.getDiskCacheDir(getApplicationContext()).toString()).setFileOperateCallback(new FileCopyAssetToSD.FileOperateCallback() {
- @Override
- public void onSuccess() {
- }
-
- @Override
- public void onFailed(String error) {
-
- }
- });
-
- // tbs内核本地加载
- // FileCopyAssetToSD.getInstance(getApplicationContext()).copyAssetsToSD("apk" , FileCopyAssetToSD.getDiskCacheDir(getApplicationContext()).toString()).setFileOperateCallback(new FileCopyAssetToSD.FileOperateCallback() {
- // @Override
- // public void onSuccess() {
- // String filePath = FileCopyAssetToSD.getDiskCacheDir(getApplicationContext()).toString() + File.separator + "x5.apk";
- // initLocalTbsCore(filePath);
- // Toast.makeText(getApplicationContext() , "复制成功"+filePath , Toast.LENGTH_LONG).show();
- //
- // }
- //
- // @Override
- // public void onFailed(String error) {
- //
- // }
- // });
-
- }
-
-
- private boolean isDownTbsSuccess = false;//TBS X5插件是否下载成功
- private int downTbsCount = 0;//尝试下载次数
- /**
- * 腾讯TBS初始化流程
- */
- public void initTBS() {
- /********************************TBS服务器下发版本开始*********************************/
- if (QbSdk.getTbsVersion(getApplicationContext()) == 0) {//获取不到版本号,说明插件没有加载成功,重新跑流程
- //判断是否是x5内核未下载成功,存在缓存 重置化sdk,这样就清除缓存继续下载了
- QbSdk.reset(getApplicationContext());
- HashMap map = new HashMap();
- map.put(TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER, true);
- map.put(TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE, true);
- QbSdk.initTbsSettings(map);
- /* 设置允许移动网络下进行内核下载。默认不下载,会导致部分一直用移动网络的用户无法使用x5内核 */
- QbSdk.setDownloadWithoutWifi(true);
-
-
-
- /* SDK内核初始化周期回调,包括 下载、安装、加载 */
- QbSdk.setTbsListener(new TbsListener() {
- /**
- * @param progress 110: 表示当前服务器认为该环境下不需要下载
- */
- @Override
- public void onDownloadFinish(int progress) {
- Log.i(TAG, "onDownloadFinished: " + progress);
- //下载结束时的状态,下载成功时errorCode为100,其他均为失败,外部不需要关注具体的失败原因
- if (isDownTbsSuccess) {
- return;
- }
- if (progress < 100) {
- return;
- }
- if (progress != 100) {//回调里面还是没有成功,那就再次尝试下载
- if (downTbsCount < 10) {//尝试下载10次,失败就开始从自己的服务器端下载
- downTbs();
- } else {//加载从自己的服务器端下载的x5内核
- FileCopyAssetToSD.getInstance(getApplicationContext()).copyAssetsToSD("apk" , FileCopyAssetToSD.getDiskCacheDir(getApplicationContext()).toString()).setFileOperateCallback(new FileCopyAssetToSD.FileOperateCallback() {
- @Override
- public void onSuccess() {
- String filePath = FileCopyAssetToSD.getDiskCacheDir(getApplicationContext()).toString() + File.separator + "x5.apk";
- initLocalTbsCore(filePath);
- Toast.makeText(getApplicationContext() , "复制成功" , Toast.LENGTH_LONG).show();
- Log.i(TAG,"复制成功:"+filePath);
-
- }
-
- @Override
- public void onFailed(String error) {
-
- }
- });
- }
- downTbsCount++;
- }
- if (progress == 100) {
- isDownTbsSuccess = true;
- }
- }
-
- /**initX5Environment
- * @param stateCode 200、232安装成功
- */
- @Override
- public void onInstallFinish(int stateCode) {
- Log.i(TAG, "onInstallFinished: " + stateCode);
- }
-
- /**
- * 首次安装应用,会触发内核下载,此时会有内核下载的进度回调。
- * @param progress 0 - 100
- */
- @Override
- public void onDownloadProgress(int progress) {
- Log.i(TAG, "Core Downloading: " + progress);
- }
- });
-
-
-
- /* 此过程包括X5内核的下载、预初始化,接入方不需要接管处理x5的初始化流程,希望无感接入 */
- QbSdk.initX5Environment(this, new PreInitCallback() {
- @Override
- public void onCoreInitFinished() {
- // 内核初始化完成,可能为系统内核,也可能为系统内核
- }
-
- /**
- * 预初始化结束
- * 由于X5内核体积较大,需要依赖wifi网络下发,所以当内核不存在的时候,默认会回调false,此时将会使用系统内核代替
- * 内核下发请求发起有24小时间隔,卸载重装、调整系统时间24小时后都可重置
- * @param isX5 是否使用X5内核
- */
- @Override
- public void onViewInitFinished(boolean isX5) {
- Log.i(TAG, "onViewInitFinished: " + isX5);
- }
- });
- }
- }
-
-
-
-
- /**
- * 下载TBS插件 通过腾讯端下发
- */
- public void downTbs() {
- //判断是否是x5内核未下载成功,存在缓存 重置化sdk,这样就清除缓存继续下载了
- QbSdk.reset(getApplicationContext());
- //开始下载x5内核
- TbsDownloader.startDownload(getApplicationContext());
- }
-
-
- /**
- * tbs内核本地加载
- */
- public void initLocalTbsCore(String filePath) {
- QbSdk.reset(getApplicationContext());
- QbSdk.setTbsListener(new TbsListener() {
- @Override
- public void onDownloadFinish(int i) {
-
- }
-
- @Override
- public void onInstallFinish(int i) {
- int tbsVersion = QbSdk.getTbsVersion(getApplicationContext());
- }
-
- @Override
- public void onDownloadProgress(int i) {
- }
- });
- QbSdk.installLocalTbsCore(getApplicationContext(), 44197, filePath);
- }
-
- }
- package com.demo.tbsloadfile;
-
- import androidx.appcompat.app.AppCompatActivity;
- import pub.devrel.easypermissions.EasyPermissions;
-
- import android.Manifest;
- import android.content.Context;
- import android.os.Bundle;
- import android.os.Environment;
- import android.text.TextUtils;
- import android.util.Log;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.LinearLayout;
-
- import com.tencent.smtt.sdk.TbsReaderView;
-
- import java.io.File;
- import java.util.logging.Logger;
-
- public class MainActivity extends AppCompatActivity implements TbsReaderView.ReaderCallback{
-
- String TAG = "MainActivity";
- String mFilePath = "";
- LinearLayout llBoard;
-
- TbsReaderView tbsReaderView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- checkPermission();
- mFilePath = FileCopyAssetToSD.getDiskCacheDir(this)+ File.separator + "安全教育(1).ppt";
-
- llBoard = findViewById(R.id.llBoard);
- findViewById(R.id.btnAdd).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
-
-
- try {
- tbsReaderView = new TbsReaderView(MainActivity.this, MainActivity.this);
- tbsReaderView.setFocusable(false);
- llBoard.addView(tbsReaderView, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
-
- String tbsReaderTemp = getDiskCacheDir(MainActivity.this).toString() + "/" + "TbsReaderTemp";
- File tbsReaderTempFile = new File(tbsReaderTemp);
-
- if (!tbsReaderTempFile.exists()) {
- tbsReaderTempFile.mkdir();
- }
-
- Bundle localBundle = new Bundle();
- localBundle.putString(TbsReaderView.KEY_FILE_PATH, mFilePath);
- localBundle.putString(TbsReaderView.KEY_TEMP_PATH, tbsReaderTemp);
- boolean bool = tbsReaderView.preOpen(getFileType(mFilePath), false);
- if (bool) {
- tbsReaderView.openFile(localBundle);
- } else {
- Log.i(TAG,"X5内核加载课件失败,需要重新加载");
- //加载本地x5so 执行 DemoApplication initLocalTbsCore();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
-
-
- @Override
- public void onCallBackAction(Integer integer, Object o, Object o1) {
- }
-
-
-
-
-
- /***
- * 获取文件类型
- *
- * @param paramString
- * @return
- */
- private String getFileType(String paramString) {
- String str = "";
-
- if (TextUtils.isEmpty(paramString)) {
- return str;
- }
- int i = paramString.lastIndexOf('.');
- if (i <= -1) {
- return str;
- }
-
-
- str = paramString.substring(i + 1);
- return str;
- }
-
- // /路径例如: /SD卡/Android/data/程序的包名/cache/uniqueName
- public static File getDiskCacheDir(Context context) {//获取缓目录
- String cachePath;
- if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
- || !Environment.isExternalStorageRemovable()) {
- cachePath = context.getExternalCacheDir().getPath();
- } else {
- cachePath = context.getCacheDir().getPath();
- }
-
- File file = new File(cachePath + File.separator + "tbs");
- if (!file.exists()) {
- file.mkdirs();
- }
- return file;
- }
-
- public void checkPermission(){
-
- String[] perms = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE,
- Manifest.permission.RECORD_AUDIO, Manifest.permission.READ_EXTERNAL_STORAGE};
- if (EasyPermissions.hasPermissions(this, perms)) {
-
- } else {
- // Do not have permissions, request them now
- EasyPermissions.requestPermissions(this, "需要先获取相关权限",
- 0x01, perms);
- }
- }
-
- @Override
- protected void onDestroy() {
- super.onDestroy();
- if (tbsReaderView != null)
- tbsReaderView.onStop();
- }
- }
参考:
腾讯TBS浏览服务打开word.pdf.ppt等文档的使用_Huang_SS的博客-CSDN博客
Android实现复制Assets文件到SD卡 - 码农教程 demo地址: https://gitee.com/woshiluchi/tbsloadfile.git 安卓腾讯TBS加载本地文件(word.pdf.ppt)-Android文档类资源-CSDN下载
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。