使用騰訊TBS打開office

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考入自己的項目

 

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

在Application中初始化x5內核,需要下載約30M的文件,如果你裝了騰訊系的軟件的話,可以不用下載。

瀏覽文件的頁面,我是通過上一個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 ();
    }
}

以上就是全部內容。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章