android {
......................
ndk {
abiFilters "armeabi", "armeabi-v7a", "x86", "mips"
}
}
private void initX5WebView() {
QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {
@Override
public void onViewInitFinished(boolean arg0) {
// TODO Auto-generated method stub
//x5內核初始化完成的回调,为true表示x5内核加载成功,否则表示x5内核加载失败,会自动切换到系统内核。
Log.d("x5", " onViewInitFinished is " + arg0);
}
@Override
public void onCoreInitFinished() {
// TODO Auto-generated method stub
}
};
QbSdk.setTbsListener(new TbsListener() {
@Override
public void onDownloadFinish(int i) {
Log.d("app", " onDownloadFinish ");
}
@Override
public void onInstallFinish(int i) {
Log.d("app", " onViewInitFinished ");
}
@Override
public void onDownloadProgress(int i) {
Log.d("app", " onViewInitFinished ");
}
});
//x5内核初始化接口
QbSdk.initX5Environment(getApplicationContext(), cb);
}
package com.pdxx.cdzp.common;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.pdxx.cdzp.app.util.FileUtil;
import com.pdxx.cdzp.app.util.ToastUtil;
import com.pdxx.cdzp.base.BaseActivity;
import com.standard.app.R;
import com.tencent.smtt.sdk.TbsReaderView;
import java.io.File;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
/**
* Created by Administrator on 2018/5/18.
*/
public class TbsReadActivity extends BaseActivity implements TbsReaderView.ReaderCallback {
@BindView(R.id.iv_back)
ImageView back;
@BindView(R.id.tv_title)
TextView title;
@BindView(R.id.tv_right)
TextView local;
@BindView(R.id.progressBar)
ProgressBar progressBar;
@BindView(R.id.rootView)
LinearLayout rootView;
private TbsReaderView readerView;
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.activity_x5web_new);
ButterKnife.bind(this);
Bundle extras = getIntent().getExtras();
String titleStr = getIntent().getStringExtra("title");
String realname = getIntent().getStringExtra("realname");
title.setText(titleStr);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
readerView = new TbsReaderView(this, this);
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
rootView.addView(readerView, params);
boolean result = readerView.preOpen(parseFormat(realname), false);
if (result) {
readerView.openFile(extras);
} else {
try {
File file = new File(Environment.getExternalStorageDirectory() + "/ruijing", realname);
Intent intent = new Intent("android.intent.action.VIEW");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addCategory("android.intent.category.DEFAULT");
String type = FileUtil.getMIMEType(file.getName());
intent.setDataAndType(Uri.fromFile(file), type);
startActivity(intent);
} catch (Exception e) {
ToastUtil.showToastLong("文件解析失败且没有找到应用可以打开此文件类型,请下载微信后再试或者下载第三方应用打开文件!");
e.printStackTrace();
}
}
}
public static void startActivity(Context context, String filePath, String realname, String title) {
File bsReaderTempFile = new File(Environment.getExternalStorageDirectory().getPath() + "/TbsReaderTemp");
if (!bsReaderTempFile.exists()) {
boolean mkdir = bsReaderTempFile.mkdir();
if (!mkdir) {
Log.e("lmx", "创建/storage/emulated/0/TbsReaderTemp失败!!!!!");
}
}
Intent intent = new Intent(context, TbsReadActivity.class);
Bundle bundle = new Bundle();
bundle.putString("filePath", filePath);
bundle.putString("tempPath", Environment.getExternalStorageDirectory().getPath());
intent.putExtras(bundle);
intent.putExtra("title", title);
intent.putExtra("realname", realname);
context.startActivity(intent);
}
@OnClick(R.id.iv_back)
public void onViewClicked() {
onBackPressed();
}
@Override
public void onCallBackAction(Integer integer, Object o, Object o1) {
}
@Override
public void onDestroy() {
super.onDestroy();
readerView.onStop();
}
private String parseFormat(String fileName) {
return fileName.substring(fileName.lastIndexOf(".") + 1);
}
}
package com.pdxx.cdzp.app.util;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import com.pdxx.cdzp.common.TbsReadActivity;
import java.io.File;
/**
* Created by Administrator on 2018/5/17.
*/
public class FileUtil {
/**
* 根据文件扩展名获取打开类型
*/
public static String getMIMEType(String fileName) {
String type = "";
String end = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()).toLowerCase();
if (end.equals("doc") || end.equals("docx")) {
type = "application/msword";
} else if (end.equals("xls") || end.equals("xlsx")) {
type = "application/vnd.ms-excel";
} else if (end.equals("ppt") || end.equals("pptx")) {
type = "application/vnd.ms-powerpoint";
} else if (end.equals("txt") || end.equals("log") || end.equals("xml")) {
type = "text/plain";
} else if (end.equals("pdf")) {
type = "application/pdf";
} else if (end.equals("rtf")) {
type = "application/rtf";
} else if (end.equals("m4a") || end.equals("mp3") || end.equals("mid") || end.equals("xmf") || end.equals("ogg") || end.equals("wav")) {
type = "audio/*";
} else if (end.equals("3gp") || end.equals("mp4") || end.equals("avi")) {
type = "video/*";
} else if (end.equals("jpg") || end.equals("gif") || end.equals("png") || end.equals("jpeg") || end.equals("bmp") || end.equals("ico")) {
type = "image/*";
} else if (end.equals("apk")) {
type = "application/vnd.android.package-archive";
} else if (end.equals("chm")) {
type = "application/x-chm";
} else if (end.equals("htm") || end.equals("html")) {
type = "text/html";
} else if (end.equals("zip") || end.equals("rar")) {
type = "application/x-gzip";
}
return type;
}
/**
* 按类型打开文件
**/
public static void openFile(Context context, File f, String filename) {
if (f.length() > 0) {
try {
String name = f.getName();
String type = "";
String end = name.substring(name.lastIndexOf(".") + 1, name.length()).toLowerCase();
if (end.equals("doc") || end.equals("docx") || end.equals("xls") || end.equals("xlsx") || end.equals("ppt")
|| end.equals("pptx") || end.equals("pdf")
) {
//如果是文档类型,直接调用X5webview打开
TbsReadActivity.startActivity(context, f.getAbsolutePath(), f.getName(), filename);
} else {
Intent intent = new Intent("android.intent.action.VIEW");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addCategory("android.intent.category.DEFAULT");
type = getMIMEType(name);
intent.setDataAndType(Uri.fromFile(f), type);
context.startActivity(intent);
}
} catch (Exception e) {
// Toast.makeText(context, context.getString(R.string.filemanager_nosupportfile), Toast.LENGTH_SHORT);
ToastUtil.showToast("没有可以打开文件的方式,请下载第三方应用打开文件!");
e.printStackTrace();
}
} else {
ToastUtil.showToast("没有可以打开文件的方式,请下载第三方应用打开文件!");
// Toast.makeText(context, context.getString(R.string.filemanager_nosupportfile), Toast.LENGTH_SHORT);
}
}
public static String getFilePathByUri(Context context, Uri uri) throws Exception {
String path = null;
// 以 file:// 开头的
if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
path = uri.getPath();
return path;
}
// 以 content:// 开头的,比如 content://media/extenral/images/media/17766
if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme()) && Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
Cursor cursor = context.getContentResolver().query(uri, new String[]{MediaStore.Images.Media.DATA}, null, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
if (columnIndex > -1) {
path = cursor.getString(columnIndex);
}
}
cursor.close();
}
return path;
}
// 4.4及之后的 是以 content:// 开头的,比如 content://com.android.providers.media.documents/document/image%3A235700
if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme()) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (DocumentsContract.isDocumentUri(context, uri)) {
if (isExternalStorageDocument(uri)) {
// ExternalStorageProvider
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
path = Environment.getExternalStorageDirectory() + "/" + split[1];
return path;
}
} else if (isDownloadsDocument(uri)) {
// DownloadsProvider
final String id = DocumentsContract.getDocumentId(uri);
Uri contentUri = null;
try {
contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
Long.valueOf(id));
} catch (Exception e) {
e.printStackTrace();
}
path = getDataColumn(context, contentUri, null, null);
return path;
} else if (isMediaDocument(uri)) {
// MediaProvider
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[]{split[1]};
path = getDataColumn(context, contentUri, selection, selectionArgs);
return path;
}
}
}
return null;
}
private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {column};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
private static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
private static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
private static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
}
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
final ImgFileBean fileBean = (ImgFileBean) adapter.getData().get(position);
File file = new File(Environment.getExternalStorageDirectory() + "/ruijing",
fileBean.fileName + "." + fileBean.fileType);
if (file.exists()) {
FileUtil.openFile(getContext(), file, fileBean.fileName + "." + fileBean.fileType);
} else {
//成功后下载
downFile(fileBean);
}
}
private void downFile(final ImgFileBean fileBean) {
pd.setMessage("正在下载");
OkGo.post(Url.DOWN_FILE).tag(this)
.params("userFlow", SPUtil.getUserFlow())
.params("fileFlow", fileBean.fileFlow)
.execute(new FileCallback(Environment.getExternalStorageDirectory() + "/ruijing", fileBean.fileName + "." + fileBean.fileType) {
@Override
public void onStart(Request request) {
super.onStart(request);
pd.show();
}
@Override
public void onFinish() {
super.onFinish();
pd.dismiss();
}
@Override
public void downloadProgress(Progress progress) {
super.downloadProgress(progress);
pd.setMax(100);
pd.setProgress((int) (progress.fraction * 100));
}
@Override
public void onSuccess(Response response) {
File body = response.body();
FileUtil.openFile(getContext(), body, fileBean.fileName+"."+fileBean.fileType);
}
});
}
fileutil.openfile (), 在传入文件的时候 , 需要后缀 , 不然就不能识别 , 这个在不同的地方自己配置