美文网首页
使用腾讯TBSWebView加载本地PDF文件

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

作者: yannyqin | 来源:发表于2018-06-07 11:24 被阅读419次

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{

        }

相关文章

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/akawsftx.html