当前位置:   article > 正文

腾讯TBS浏览服务打开本地文档(word.pdf.ppt)踩过的坑_tbsreaderview

tbsreaderview

记录一下安卓使用腾讯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时无法下载内核?


下面附上代码段:

  1. package com.demo.tbsloadfile;
  2. import android.app.Application;
  3. import android.util.Log;
  4. import android.widget.Toast;
  5. import com.tencent.smtt.export.external.TbsCoreSettings;
  6. import com.tencent.smtt.sdk.QbSdk;
  7. import com.tencent.smtt.sdk.QbSdk.PreInitCallback;
  8. import com.tencent.smtt.sdk.TbsDownloader;
  9. import com.tencent.smtt.sdk.TbsListener;
  10. import java.io.File;
  11. import java.util.HashMap;
  12. public class DemoApplication extends Application {
  13. private static final String TAG = "DemoApplication";
  14. @Override
  15. public void onCreate() {
  16. super.onCreate();
  17. initTBS();
  18. //targetSdkVersion > 27 ,需要参考文档 3.7 targetAPI为Android P时无法下载内核?添加xml
  19. // https://x5.tencent.com/docs/questions.html
  20. //复制文件到sdkcard
  21. FileCopyAssetToSD.getInstance(getApplicationContext()).copyAssetsToSD("sources" , FileCopyAssetToSD.getDiskCacheDir(getApplicationContext()).toString()).setFileOperateCallback(new FileCopyAssetToSD.FileOperateCallback() {
  22. @Override
  23. public void onSuccess() {
  24. }
  25. @Override
  26. public void onFailed(String error) {
  27. }
  28. });
  29. // tbs内核本地加载
  30. // FileCopyAssetToSD.getInstance(getApplicationContext()).copyAssetsToSD("apk" , FileCopyAssetToSD.getDiskCacheDir(getApplicationContext()).toString()).setFileOperateCallback(new FileCopyAssetToSD.FileOperateCallback() {
  31. // @Override
  32. // public void onSuccess() {
  33. // String filePath = FileCopyAssetToSD.getDiskCacheDir(getApplicationContext()).toString() + File.separator + "x5.apk";
  34. // initLocalTbsCore(filePath);
  35. // Toast.makeText(getApplicationContext() , "复制成功"+filePath , Toast.LENGTH_LONG).show();
  36. //
  37. // }
  38. //
  39. // @Override
  40. // public void onFailed(String error) {
  41. //
  42. // }
  43. // });
  44. }
  45. private boolean isDownTbsSuccess = false;//TBS X5插件是否下载成功
  46. private int downTbsCount = 0;//尝试下载次数
  47. /**
  48. * 腾讯TBS初始化流程
  49. */
  50. public void initTBS() {
  51. /********************************TBS服务器下发版本开始*********************************/
  52. if (QbSdk.getTbsVersion(getApplicationContext()) == 0) {//获取不到版本号,说明插件没有加载成功,重新跑流程
  53. //判断是否是x5内核未下载成功,存在缓存 重置化sdk,这样就清除缓存继续下载了
  54. QbSdk.reset(getApplicationContext());
  55. HashMap map = new HashMap();
  56. map.put(TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER, true);
  57. map.put(TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE, true);
  58. QbSdk.initTbsSettings(map);
  59. /* 设置允许移动网络下进行内核下载。默认不下载,会导致部分一直用移动网络的用户无法使用x5内核 */
  60. QbSdk.setDownloadWithoutWifi(true);
  61. /* SDK内核初始化周期回调,包括 下载、安装、加载 */
  62. QbSdk.setTbsListener(new TbsListener() {
  63. /**
  64. * @param progress 110: 表示当前服务器认为该环境下不需要下载
  65. */
  66. @Override
  67. public void onDownloadFinish(int progress) {
  68. Log.i(TAG, "onDownloadFinished: " + progress);
  69. //下载结束时的状态,下载成功时errorCode为100,其他均为失败,外部不需要关注具体的失败原因
  70. if (isDownTbsSuccess) {
  71. return;
  72. }
  73. if (progress < 100) {
  74. return;
  75. }
  76. if (progress != 100) {//回调里面还是没有成功,那就再次尝试下载
  77. if (downTbsCount < 10) {//尝试下载10次,失败就开始从自己的服务器端下载
  78. downTbs();
  79. } else {//加载从自己的服务器端下载的x5内核
  80. FileCopyAssetToSD.getInstance(getApplicationContext()).copyAssetsToSD("apk" , FileCopyAssetToSD.getDiskCacheDir(getApplicationContext()).toString()).setFileOperateCallback(new FileCopyAssetToSD.FileOperateCallback() {
  81. @Override
  82. public void onSuccess() {
  83. String filePath = FileCopyAssetToSD.getDiskCacheDir(getApplicationContext()).toString() + File.separator + "x5.apk";
  84. initLocalTbsCore(filePath);
  85. Toast.makeText(getApplicationContext() , "复制成功" , Toast.LENGTH_LONG).show();
  86. Log.i(TAG,"复制成功:"+filePath);
  87. }
  88. @Override
  89. public void onFailed(String error) {
  90. }
  91. });
  92. }
  93. downTbsCount++;
  94. }
  95. if (progress == 100) {
  96. isDownTbsSuccess = true;
  97. }
  98. }
  99. /**initX5Environment
  100. * @param stateCode 200232安装成功
  101. */
  102. @Override
  103. public void onInstallFinish(int stateCode) {
  104. Log.i(TAG, "onInstallFinished: " + stateCode);
  105. }
  106. /**
  107. * 首次安装应用,会触发内核下载,此时会有内核下载的进度回调。
  108. * @param progress 0 - 100
  109. */
  110. @Override
  111. public void onDownloadProgress(int progress) {
  112. Log.i(TAG, "Core Downloading: " + progress);
  113. }
  114. });
  115. /* 此过程包括X5内核的下载、预初始化,接入方不需要接管处理x5的初始化流程,希望无感接入 */
  116. QbSdk.initX5Environment(this, new PreInitCallback() {
  117. @Override
  118. public void onCoreInitFinished() {
  119. // 内核初始化完成,可能为系统内核,也可能为系统内核
  120. }
  121. /**
  122. * 预初始化结束
  123. * 由于X5内核体积较大,需要依赖wifi网络下发,所以当内核不存在的时候,默认会回调false,此时将会使用系统内核代替
  124. * 内核下发请求发起有24小时间隔,卸载重装、调整系统时间24小时后都可重置
  125. * @param isX5 是否使用X5内核
  126. */
  127. @Override
  128. public void onViewInitFinished(boolean isX5) {
  129. Log.i(TAG, "onViewInitFinished: " + isX5);
  130. }
  131. });
  132. }
  133. }
  134. /**
  135. * 下载TBS插件 通过腾讯端下发
  136. */
  137. public void downTbs() {
  138. //判断是否是x5内核未下载成功,存在缓存 重置化sdk,这样就清除缓存继续下载了
  139. QbSdk.reset(getApplicationContext());
  140. //开始下载x5内核
  141. TbsDownloader.startDownload(getApplicationContext());
  142. }
  143. /**
  144. * tbs内核本地加载
  145. */
  146. public void initLocalTbsCore(String filePath) {
  147. QbSdk.reset(getApplicationContext());
  148. QbSdk.setTbsListener(new TbsListener() {
  149. @Override
  150. public void onDownloadFinish(int i) {
  151. }
  152. @Override
  153. public void onInstallFinish(int i) {
  154. int tbsVersion = QbSdk.getTbsVersion(getApplicationContext());
  155. }
  156. @Override
  157. public void onDownloadProgress(int i) {
  158. }
  159. });
  160. QbSdk.installLocalTbsCore(getApplicationContext(), 44197, filePath);
  161. }
  162. }
 
  1. package com.demo.tbsloadfile;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import pub.devrel.easypermissions.EasyPermissions;
  4. import android.Manifest;
  5. import android.content.Context;
  6. import android.os.Bundle;
  7. import android.os.Environment;
  8. import android.text.TextUtils;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.widget.LinearLayout;
  13. import com.tencent.smtt.sdk.TbsReaderView;
  14. import java.io.File;
  15. import java.util.logging.Logger;
  16. public class MainActivity extends AppCompatActivity implements TbsReaderView.ReaderCallback{
  17. String TAG = "MainActivity";
  18. String mFilePath = "";
  19. LinearLayout llBoard;
  20. TbsReaderView tbsReaderView;
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25. checkPermission();
  26. mFilePath = FileCopyAssetToSD.getDiskCacheDir(this)+ File.separator + "安全教育(1).ppt";
  27. llBoard = findViewById(R.id.llBoard);
  28. findViewById(R.id.btnAdd).setOnClickListener(new View.OnClickListener() {
  29. @Override
  30. public void onClick(View view) {
  31. try {
  32. tbsReaderView = new TbsReaderView(MainActivity.this, MainActivity.this);
  33. tbsReaderView.setFocusable(false);
  34. llBoard.addView(tbsReaderView, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
  35. String tbsReaderTemp = getDiskCacheDir(MainActivity.this).toString() + "/" + "TbsReaderTemp";
  36. File tbsReaderTempFile = new File(tbsReaderTemp);
  37. if (!tbsReaderTempFile.exists()) {
  38. tbsReaderTempFile.mkdir();
  39. }
  40. Bundle localBundle = new Bundle();
  41. localBundle.putString(TbsReaderView.KEY_FILE_PATH, mFilePath);
  42. localBundle.putString(TbsReaderView.KEY_TEMP_PATH, tbsReaderTemp);
  43. boolean bool = tbsReaderView.preOpen(getFileType(mFilePath), false);
  44. if (bool) {
  45. tbsReaderView.openFile(localBundle);
  46. } else {
  47. Log.i(TAG,"X5内核加载课件失败,需要重新加载");
  48. //加载本地x5so 执行 DemoApplication initLocalTbsCore();
  49. }
  50. } catch (Exception e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. });
  55. }
  56. @Override
  57. public void onCallBackAction(Integer integer, Object o, Object o1) {
  58. }
  59. /***
  60. * 获取文件类型
  61. *
  62. * @param paramString
  63. * @return
  64. */
  65. private String getFileType(String paramString) {
  66. String str = "";
  67. if (TextUtils.isEmpty(paramString)) {
  68. return str;
  69. }
  70. int i = paramString.lastIndexOf('.');
  71. if (i <= -1) {
  72. return str;
  73. }
  74. str = paramString.substring(i + 1);
  75. return str;
  76. }
  77. // /路径例如: /SD/Android/data/程序的包名/cache/uniqueName
  78. public static File getDiskCacheDir(Context context) {//获取缓目录
  79. String cachePath;
  80. if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
  81. || !Environment.isExternalStorageRemovable()) {
  82. cachePath = context.getExternalCacheDir().getPath();
  83. } else {
  84. cachePath = context.getCacheDir().getPath();
  85. }
  86. File file = new File(cachePath + File.separator + "tbs");
  87. if (!file.exists()) {
  88. file.mkdirs();
  89. }
  90. return file;
  91. }
  92. public void checkPermission(){
  93. String[] perms = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE,
  94. Manifest.permission.RECORD_AUDIO, Manifest.permission.READ_EXTERNAL_STORAGE};
  95. if (EasyPermissions.hasPermissions(this, perms)) {
  96. } else {
  97. // Do not have permissions, request them now
  98. EasyPermissions.requestPermissions(this, "需要先获取相关权限",
  99. 0x01, perms);
  100. }
  101. }
  102. @Override
  103. protected void onDestroy() {
  104. super.onDestroy();
  105. if (tbsReaderView != null)
  106. tbsReaderView.onStop();
  107. }
  108. }

参考:
腾讯TBS浏览服务打开word.pdf.ppt等文档的使用_Huang_SS的博客-CSDN博客

Android实现复制Assets文件到SD卡 - 码农教程


demo地址:
https://gitee.com/woshiluchi/tbsloadfile.git

安卓腾讯TBS加载本地文件(word.pdf.ppt)-Android文档类资源-CSDN下载

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/175054
推荐阅读
相关标签
  

闽ICP备14008679号