使用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,622评论 13 48
  • 笔者最近两个项目里头都有需要展示文件的功能,于是做了一番调研,发现asce1885给出一份方案,不过都是关于pdf...
    北冥有鱼1129阅读 2,193评论 2 7
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,010评论 19 139
  • Spring Web MVC Spring Web MVC 是包含在 Spring 框架中的 Web 框架,建立于...
    Hsinwong阅读 22,600评论 1 92
  • 好好学习,好好努力,好好生活,苦难很多,日子很长。不要失望,不要失望,不要失望。
    纪六六呀阅读 183评论 0 0