使用腾讯TBSWebView加载本地PDF文件

1:在xml文件里面写入父布局

    <RelativeLayout
        android:id="@+id/tbsView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></RelativeLayout>

2:定义TbsReaderView 使用java加入到父布局和文件缓存路径

   mTbsReaderView = new TbsReaderView(ReportPdfViewActivity.this, new TbsReaderView.ReaderCallback()
        {
            @Override
            public void onCallBackAction(Integer integer, Object o, Object o1)
            {

            }
        });

  private String tbsReaderTemp = Environment.getExternalStorageDirectory() + "/TbsReaderTemp";
  mRelativeLayout.addView(mTbsReaderView,new RelativeLayout.LayoutParams(-1,-1));

3:获取文件类型

 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;
    }

4:根据文件路径、缓存路径、文件类型创建Bundle并使用TbsReaderView 打开


        //增加下面一句解决没有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);
        if (result) {
            mTbsReaderView.openFile(bundle);
        }else{

        }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,982评论 19 139
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,144评论 1 32
  • 二姨和二姨夫去泡温泉做疗养了。 大门上挂了大铁锁,窗户拉上了窗帘。哥哥姐姐的车不再在门口停成一团。 门口静了。 二...
    塔塔妹阅读 446评论 2 3
  • “毕竟答案不止一个,我们不是神,什么是对的,什么是正确答案,我们或许一辈子都无法知道。” “但就算无法知道,也只能...
    叶方翳蝉阅读 1,574评论 0 1
  • 今天早上在酒店一醒来,就听到外面下起了雨,看看窗外雨下的还不小,真是喜雨,春雨贵如油,田地里的庄稼要饱饱...
    宏宏203阅读 341评论 0 0