TBS是腾讯的浏览服务,依托x5内核,对于普通第三方开发者完全免费,使用也没有限制。

官网地址:https://x5.tencent.com/

关于Android 上打开office文档的解决方案参考:https://blog.csdn.net/u011791526/article/details/82994750

not supported by:docx、not supported by:pdf解决:https://github.com/ZhongXiaoHong/superFileView/issues/10

使用过程:

首先把从官网下载的腾讯SDK中的jar和jniLibs考入自己的项目

 使用腾讯TBS打开office

然后就可以愉快的使用啦。。。

在Application中初始化x5内核,需要下载约30M的文件,如果你装了腾讯系的软件的话,可以不用下载。

使用腾讯TBS打开office

浏览文件的页面,我是通过上一个Activity将获取到文件路径传递到此页面以显示,本来是想在一个页面的,但是每次浏览第一个

文件后,浏览第二个文件就会处于一直加载中,mTbsReaderView.onStop (),当前页面调用此方法也没有用,最好的就是跳一个页面,每次展示完退出当前显示页面时在onDestroy()里调用mTbsReaderView.onStop (),这是最合适的。

package com.tbs_or_excel;

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.tbs_or_excel.util.FileUtil;
import com.tencent.smtt.sdk.TbsReaderView;

import java.io.File;

import autopair.com.tbs_or_excel.R;

public class DisplayOfficeActivity extends AppCompatActivity implements TbsReaderView.ReaderCallback {

    private TbsReaderView mTbsReaderView;
    private String mFileUrl=Environment.getExternalStorageDirectory () + "/temp/new_scan.docx";
    private String mFileName="";
    private RelativeLayout rootRl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_excel);


        Intent intent = getIntent ();
        String path = intent.getStringExtra ("path");
        Log.e ("a", "path: "+path);
        mFileName = parseName(path);
        mTbsReaderView=new TbsReaderView (this, this);
        rootRl= findViewById (R.id.rl_root);
        rootRl.addView (mTbsReaderView, new RelativeLayout.LayoutParams (RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
        displayFile();

    }


    /**
     * 这个暂时没什么用
     * @param integer
     * @param o
     * @param o1
     */
    @Override
    public void onCallBackAction(Integer integer, Object o, Object o1) {

    }

    /**
     * 显示
     */
    private void displayFile() {
        Log.e ("a", "displayFile: " + getLocalFile ().getPath ());

        Bundle bundle=new Bundle ();
        bundle.putString ("filePath", getLocalFile ().getPath ());
        bundle.putString ("tempPath", Environment.getExternalStorageDirectory ().getPath ());
        boolean result=mTbsReaderView.preOpen (FileUtil.getFileType (mFileName), false);
        if (result) {
            mTbsReaderView.openFile (bundle);
        }
    }

    /**
     * 获取文件名
     * @param url
     * @return
     */
    private String parseName(String url) {
        String fileName=null;
        try {
            fileName=url.substring (url.lastIndexOf ("/") + 1);
        } finally {
            if (TextUtils.isEmpty (fileName)) {
                fileName=String.valueOf (System.currentTimeMillis ());
            }
        }
        return fileName;
    }

    /**
     * 获取本地文件
     * @return
     */
    private File getLocalFile() {
        //  return new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), mFileName);
        return new File (Environment.getExternalStorageDirectory () + "/temp", mFileName);
    }

    @Override
    protected void onStop() {
        super.onStop ();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy ();
        mTbsReaderView.onStop ();
    }
}

以上就是全部内容。

相关文章: