基于騰訊x5的文檔閱讀器

前言

鵝廠的東西確實是好東西,文檔也確實真的爛

x5WebView加載本地文檔

x5是騰訊用來替代Andorid系統(tǒng)webview,具備打開本地文檔的功能。具體文檔請移步:https://x5.tencent.com/tbs/index.html

首先接入x5

具體步驟

打開文檔的核心代碼

 public void openFile(Activity activity, String path) {
        if (tbsReaderView == null) {
            tbsReaderView = new TbsReaderView(activity, new TbsReaderView.ReaderCallback() {
                @Override
                public void onCallBackAction(Integer integer, Object o, Object o1) {
                }
            });

            addView(tbsReaderView, new LinearLayout.LayoutParams(-1, -1));
        }

        String bsReaderTemp = "/storage/emulated/0/TbsReaderTemp";
        File bsReaderTempFile = new File(bsReaderTemp);
        if (!bsReaderTempFile.exists()) {
            boolean mkdir = bsReaderTempFile.mkdir();
            if (!mkdir) {
                Toast.makeText(getContext(), "發(fā)生了錯誤,請稍后重試", Toast.LENGTH_SHORT).show();
                return;
            }
        }
        //加載文件
        Bundle localBundle = new Bundle();
        localBundle.putString("filePath", path);
        localBundle.putString("tempPath", Environment.getExternalStorageDirectory() + "/" +
                "TbsReaderTemp");

        boolean canOpen = tbsReaderView.preOpen(getFileType(path), false);
        if (canOpen) {
            tbsReaderView.openFile(localBundle);
        } else {
            Toast.makeText(getContext(), "無法打開此文件", Toast.LENGTH_SHORT).show();
        }

    }

注意 SDK提供的TbsReaderView無法在xml中直接使用,如果在xml中實例化會報錯,其構(gòu)造方法要求context必須是Activity

源碼如下:

    public TbsReaderView(Context var1, TbsReaderView.ReaderCallback var2) {
        super(var1.getApplicationContext());
        if (!(var1 instanceof Activity)) {
            throw new RuntimeException("error: unexpect context(none Activity)");
        } else {
            this.d = var2;
            this.a = var1;
            this.e = new bg(this);
        }
    }

我封裝了一個view

上代碼

public class DocPreview extends FrameLayout {


    private TbsReaderView tbsReaderView;

    public DocPreview(Context context) {
        this(context, null);
    }

    public DocPreview(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public DocPreview(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    public static void init(final Context context) {
        QbSdk.initX5Environment(context, new QbSdk.PreInitCallback() {
            @Override
            public void onCoreInitFinished() {

            }

            @Override
            public void onViewInitFinished(boolean b) {

                Toast.makeText(context, "加載" + b, Toast.LENGTH_SHORT).show();
            }
        });
    }


    public void openFile(Activity activity, String path) {

        if (tbsReaderView == null) {

            tbsReaderView = new TbsReaderView(activity, new TbsReaderView.ReaderCallback() {
                @Override
                public void onCallBackAction(Integer integer, Object o, Object o1) {

                }
            });

            addView(tbsReaderView, new LinearLayout.LayoutParams(-1, -1));
        }

        String bsReaderTemp = "/storage/emulated/0/TbsReaderTemp";
        File bsReaderTempFile = new File(bsReaderTemp);
        if (!bsReaderTempFile.exists()) {
            boolean mkdir = bsReaderTempFile.mkdir();
            if (!mkdir) {
                Toast.makeText(getContext(), "發(fā)生了錯誤,請稍后重試", Toast.LENGTH_SHORT).show();
                return;
            }
        }

        //加載文件
        Bundle localBundle = new Bundle();
        localBundle.putString("filePath", path);
        localBundle.putString("tempPath", Environment.getExternalStorageDirectory() + "/" +
                "TbsReaderTemp");

        boolean canOpen = tbsReaderView.preOpen(getFileType(path), false);

        if (canOpen) {
            tbsReaderView.openFile(localBundle);
        } else {
            Toast.makeText(getContext(), "無法打開此文件", Toast.LENGTH_SHORT).show();
        }
    }


    /***
     * 獲取文件類型
     *
     * @param paramString
     * @return
     */
    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;
    }


    public void onDestroy() {
        if (tbsReaderView != null) {
            tbsReaderView.onStop();
        }
    }
    
}

在使用前請先調(diào)用init 初始化 ,還有注意關(guān)閉頁面的時候調(diào)用onDestroy()
這里有幾個坑,使用的時候一定要注意已經(jīng)申請了讀寫權(quán)限,第一次加載的時候需要下載文檔插件也要注意一定要在可以上網(wǎng)的環(huán)境,我在集成的時候做測試的時候一遍就通了完全沒問題,但是集成到項目里的時候初始化總是失敗,后來反復(fù)刪了幾遍有稀里糊涂好了。

我把DocPreview做成了一個module,如果集成過程中出現(xiàn)各種奇怪錯誤,可以直接下載DocPreview然后依賴。

代碼傳送門

相關(guān)資料

Android應(yīng)用內(nèi)展示word、excel、pdf、ppt等文件
superFileView
Android 展示本地或網(wǎng)絡(luò)pdf文件

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容