需求:
做了一个教育类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();
}
}
网友评论