使用X5內核展示word、pdf文件

需求:

做了一個教育類app,神奇的是我們的資料有各種各樣的格式,之前只是pdf的時候用一個PDFView就搞定了,現在有加上了Word、Excel....

方案:

直接跳瀏覽器的做法可以,但是一票否定,因為體驗不好,接下來,該神器出場了:https://x5.tencent.com/(PS:被安卓原生瀏覽器困擾的同志們順便解救了一把)

代碼:

好了,加入jar包、so就不寫了,我覺得大家都會,這里貼一下文件展示的代碼封裝:

package XXXX.widget;

import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import xxxx.log.Logger;
import xxxx.MyToast;
import com.tencent.smtt.sdk.TbsReaderView;

import java.io.File;

/**
 * Created by GONGPENG on 2017/11/15:13:54
 */

public class TBSFileView extends FrameLayout implements TbsReaderView.ReaderCallback {

    private TbsReaderView mTbsReaderView;

    private String TAG = "TBSFileView";

    public TBSFileView(@NonNull Context context) {
        super(context);
        mTbsReaderView = new TbsReaderView(context, this);
    }

    public TBSFileView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mTbsReaderView = new TbsReaderView(context, this);
        this.addView(mTbsReaderView, new FrameLayout.LayoutParams(-1, -1));
    }

    public TBSFileView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mTbsReaderView = new TbsReaderView(context, this);
        this.addView(mTbsReaderView, new FrameLayout.LayoutParams(-1, -1));
    }


    public void displayFile(File mFile) {
        if (mFile != null && !TextUtils.isEmpty(mFile.toString())) {
            //增加下面一句解決沒有TbsReaderTemp文件夾存在導致加載文件失敗
            String bsReaderTemp = "/storage/emulated/0/TbsReaderTemp";
            File bsReaderTempFile = new File(bsReaderTemp);

            if (!bsReaderTempFile.exists()) {
                Logger.d(TAG, "準備創建/storage/emulated/0/TbsReaderTemp!!");
                boolean mkdir = bsReaderTempFile.mkdir();
                if (!mkdir) {
                    Logger.d(TAG, "創建/storage/emulated/0/TbsReaderTemp失敗!!!!!");
                }
            }

            //加載文件
            Bundle localBundle = new Bundle();
            localBundle.putString("filePath", mFile.toString());
            localBundle.putString("tempPath", Environment.getExternalStorageDirectory() + "/" + "TbsReaderTemp");
            boolean bool = this.mTbsReaderView.preOpen(getFileType(mFile.toString()), false);
            if (bool) {
                this.mTbsReaderView.openFile(localBundle);
            }
        } else {
            MyToast.showAtCenter(getContext(), "文件路徑無效!");
        }

    }

    /***
     * 獲取文件類型
     *
     * @param paramString
     * @return
     */
    private String getFileType(String paramString) {
        String str = "";

        if (TextUtils.isEmpty(paramString)) {
            Logger.d(TAG, "paramString---->null");
            return str;
        }
        Logger.d(TAG, "paramString:" + paramString);
        int i = paramString.lastIndexOf('.');
        if (i <= -1) {
            Logger.d(TAG, "i <= -1");
            return str;
        }
        str = paramString.substring(i + 1);
        Logger.d(TAG, "paramString.substring(i + 1)------>" + str);
        return str;
    }

    @Override
    public void onCallBackAction(Integer integer, Object o, Object o1) {

    }

    public void onStopDisplay() {
        if (mTbsReaderView != null) {
            mTbsReaderView.onStop();
        }
    }
}

怎么用呢?

private void displayFromFile() {
    try {
        pdfLoadView.setVisibility(View.GONE);
        String fileName = getDownloadPath() + File.separator + name + fileType;
        File file =new File(fileName);
        if (file.exists()) {
            hideErrorView();
            pdfView.displayFile(file);
        }else {
            mErrorView.setErrText("文件讀取錯誤");
            showErrorView();
        }
       e.printStackTrace();
      }
  }
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 關于這份調研報告,不是從技術角度深入探索,重點是從產品本身分析,通俗易懂才是重點。主要是為了鍛煉平時做技術調研和競...
    石先閱讀 23,579評論 13 48
  • 筆者最近兩個項目里頭都有需要展示文件的功能,于是做了一番調研,發現asce1885給出一份方案,不過都是關于pdf...
    北冥有魚1129閱讀 2,152評論 2 7
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,868評論 18 139
  • Spring Web MVC Spring Web MVC 是包含在 Spring 框架中的 Web 框架,建立于...
    Hsinwong閱讀 22,522評論 1 92
  • 好好學習,好好努力,好好生活,苦難很多,日子很長。不要失望,不要失望,不要失望。
    紀六六呀閱讀 182評論 0 0