项目clone地址:
https://github.com/mrliuwen/lw_x5_webview_flutter.git
起因:肺炎闹 产品要
实现方式: 之前在pub库里看到一个X5的 恨遗憾的实现方式是用的直接open。不是用TbsReaderView
后面我替换用TbsReaderView重写了打开文件这部分 需要自定义的懂android原生的小伙伴可以直接在原生部分修改FileActivity这个类
使用方式: 见代码
注意使用过程中关于xml的配置 详情可以借鉴 demo中android xml的配置 主要是privoder 和filepath
建议用demo地址学习
import ‘package:flutter/cupertino.dart’;
import ‘package:flutter/material.dart’;
import ‘package:x5_webview/x5_sdk.dart’;
import ‘package:dio/dio.dart’;
void main() {
X5Sdk.setDownloadWithoutWifi(true); //没有x5内核,是否在非wifi模式下载内核。默认false
X5Sdk.init().then((isOK) {
print(isOK ? “X5内核成功加载” : “X5内核加载失败”);
});
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(home: HomePage());
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State {
String localPath = ‘/storage/emulated/0/Android/data/io.morbit.morbit_flutter/files/logs.csv’; //android x5文件sdk需要正确格式 本地文件才能正确打开
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(‘X5打开本地文件示例’),
),
body: Center(
child: InkWell(
child: Text(‘打开’),
onTap: () {
X5Sdk.openFile(localPath);
},
),
),
);
}
}
原生fileActivity代码如下
package com.cjx.x5_webview;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import com.tencent.smtt.sdk.TbsReaderView;
import java.io.File;
import java.util.List;
public class FileActivity extends Activity {
private TbsReaderView mTbsReaderView;
private String filePath = "";
private String title = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_displayfile);
initTbsReaderView();
Intent intent = getIntent();
filePath = intent.getStringExtra("filepath");
ActionBar actionBar = getActionBar();
try {
title = filePath.substring(filePath.lastIndexOf("/") + 1);
} catch (Exception e) {
}
actionBar.setTitle(title);
displayFile(filePath);
}
@Override
protected void onDestroy() {
super.onDestroy();
mTbsReaderView.onStop();
}
RelativeLayout rootRl;
private void initTbsReaderView() {
mTbsReaderView = new TbsReaderView(FileActivity.this, new TbsReaderView.ReaderCallback() {
@Override
public void onCallBackAction(Integer integer, Object o, Object o1) {
}
});
rootRl = (RelativeLayout) findViewById(R.id.root_layout);
rootRl.addView(mTbsReaderView, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
}
private void displayFile(String filePath) {
boolean isCan = false;
isCan = this.mTbsReaderView.preOpen(parseFormat(filePath), false);
Log.d("FileActivity2222", isCan + "");
if (isCan) {
String bsReaderTemp = "/storage/emulated/0/TbsReaderTemp";
File bsReaderTempFile = new File(bsReaderTemp);
if (!bsReaderTempFile.exists()) {
boolean mkdir = bsReaderTempFile.mkdir();
if (!mkdir) {
Log.e("TAG", "创建/storage/emulated/0/TbsReaderTemp失败!!!!!");
}
}
Bundle bundle = new Bundle();
bundle.putString("filePath", filePath);
bundle.putString("tempPath", Environment.getExternalStorageDirectory().toString() + "/" + "TbsReaderTemp");
mTbsReaderView.openFile(bundle);
}
}
private String parseFormat(String fileName) {
return fileName.substring(fileName.lastIndexOf(".") + 1);
}
}
来源:CSDN
作者:庄童
链接:https://blog.csdn.net/qq_36071410/article/details/104677141