Kotlin android x5瀏覽器平臺加載本體ppt,pdf,dox

1.在開始之前需要先配置一下x5內核,https://x5.tencent.com/tbs/guide/sdkInit.html,參考開發文檔即可在這裏不過多贅述,下面是主要佈局代碼,activity_file_display.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_file_display"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.zjg_x5_v12.SuperFileView2
        android:id="@+id/mSuperFileView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </com.example.zjg_x5_v12.SuperFileView2>

</RelativeLayout>

這個佈局代碼就是利用SuperFileView2繼承FrameLayout進行view顯示,下面看一下 SuperFileView2.java代碼

import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.tencent.smtt.sdk.TbsReaderView;

import java.io.File;

/**
 * Created by 12457 on 2017/8/29.
 */

public class SuperFileView2 extends FrameLayout implements TbsReaderView.ReaderCallback {

    private static String TAG = "SuperFileView";
    private TbsReaderView mTbsReaderView;
    private int saveTime = -1;
    private Context context;

    public SuperFileView2(Context context) {
        this(context, null, 0);
    }

    public SuperFileView2(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SuperFileView2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mTbsReaderView = new TbsReaderView(context, this);
        this.addView(mTbsReaderView, new LinearLayout.LayoutParams(-1, -1));
        this.context = context;
    }


    private OnGetFilePathListener mOnGetFilePathListener;

    public void setOnGetFilePathListener(OnGetFilePathListener mOnGetFilePathListener) {
        this.mOnGetFilePathListener = mOnGetFilePathListener;
    }


    private TbsReaderView getTbsReaderView(Context context) {
        return new TbsReaderView(context, this);
    }

    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()) {
               //TLog.d("準備創建/storage/emulated/0/TbsReaderTemp!!");
                boolean mkdir = bsReaderTempFile.mkdir();
                if(!mkdir){
                 //   TLog.e("創建/storage/emulated/0/TbsReaderTemp失敗!!!!!");
                }
            }

            //顯示文件
            Bundle localBundle = new Bundle();
            TLog.d(mFile.toString());
            localBundle.putString("filePath", mFile.toString());

            localBundle.putString("tempPath", Environment.getExternalStorageDirectory() + "/" + "TbsReaderTemp");

            if (this.mTbsReaderView == null)
                this.mTbsReaderView = getTbsReaderView(context);
            boolean bool = this.mTbsReaderView.preOpen(getFileType(mFile.toString()), false);
            //初始化文件,然後調用
            if (bool) {
                this.mTbsReaderView.openFile(localBundle);
                //localBundle包含文件路徑,加載任務全部交由tbs內核
            }
        } else {
            TLog.e("文件路徑無效!");
        }

    }

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

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


        str = paramString.substring(i + 1);
      //  TLog.d(TAG, "paramString.substring(i + 1)------>" + str);
        return str;
    }

    public void show() {
        if(mOnGetFilePathListener!=null){
            mOnGetFilePathListener.onGetFilePath(this);
        }
    }

    /***
     * 將獲取File路徑的工作,運行tbs顯示
     */
    public interface OnGetFilePathListener {
        void onGetFilePath(SuperFileView2 mSuperFileView2);
    }


    @Override
    public void onCallBackAction(Integer integer, Object o, Object o1) {
        TLog.e("****************************************************" + integer);
    }

    public void onStopDisplay() {
        if (mTbsReaderView != null) {
            mTbsReaderView.onStop();//這裏加載完成後必須要停止tbs
        }
    }
}

mTbsReaderView.preOpen(getFileType(mFile.toString()), false)進行文件打開之前的初始化工作
調用的mTbsReaderView.openFile(localBundle)打開文件,localBundle裏包含有文件在本地的路徑,加載文件步驟十分簡單,其中的加載任務全部交由tbs內核完成。

3.FileDisplayActivity代碼

class FileDisplayActivity : AppCompatActivity() {


    //private val TAG = "FileDisplayActivity"

    private val filePath:String by lazy { intent.getStringExtra(PATH) }

    companion object{
        private const val PATH = "path"
        fun newInstance(context: Context,path:String){
            context.startActivity<FileDisplayActivity>(PATH to path)//這裏就是接受從主頁面傳過來的數據

        }
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_file_display)
        init()
    }


    fun init() {
        mSuperFileView.setOnGetFilePathListener {
            getFilePathAndShowFile(mSuperFileView)
        }
        mSuperFileView.show()

    }


    private fun getFilePathAndShowFile(mSuperFileView2: SuperFileView2) {


            mSuperFileView2.displayFile(File(filePath))//傳輸路徑
    }


    public override fun onDestroy() {
        super.onDestroy()
        TLog.d("FileDisplayActivity-->onDestroy")
        if (mSuperFileView != null) {
            mSuperFileView!!.onStopDisplay()
        }
    }




}

4.運行截圖

在這裏插入圖片描述

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