Android实现在线预览office⽂档(Word,Pdf,等格式)
版权声明:本⽂为博主原创⽂章,遵循版权协议,转载请附上原⽂出处链接和本声明。
本⽂链接:
1.概述
我们都知道,Android原⽣并没有提供浏览office⽂档格式的相关Api,在安卓端想要实现在线预览office⽂档的功能显然很是复杂,我们⼿机安装QQ浏览器时,在⼿机打开office⽂档时会提⽰如图,
这就是这篇⽂章的主⾓–腾讯X5内核(TBS)
2,什么是TBS
腾讯浏览服务(TBS,Tencent Browsing Service)整合腾讯底层浏览技术和腾讯平台资源及能⼒,提供整体浏览服务解决⽅案。腾讯浏览服务⾯向应⽤开发商和⼴⼤开发者,提供浏览增强,内容框架,⼴告体系,H5游戏分发,⼤数据等服务,能够帮助应⽤开发商⼤幅改善应⽤体验,有效提升开发,运营,商业化的效率。
我们主要介绍的是他的⽂件打开能⼒
⽬前⽀持42种⽂件格式,包括20种⽂档、12种⾳乐、6种图⽚和4种压缩包。帮助应⽤实现应⽤内⽂档浏览,⽆需跳转调⽤其他应⽤。
详细介绍见
3.加载txt格式的效果图
注意加载的过程,是先加载⽀持txt格式的插件,然后把txt⽂档加载出来的
4.TBS的使⽤预览office⽂件
详细的使⽤见
技术⽂档中并没有提供浏览⽂件相关的信息
核⼼类就⼀个 TbsReaderView 继承⾃FrameLayout 由于使⽤起来⽐较简单,我就直接贴代码了,请看注释
布局⽂件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="schemas.android/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:onClick="onClick"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/colorAccent"
android:text="请选择⽂件打开"/>
<FrameLayout
android:id="@+id/fl"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
</LinearLayout>
java代码实现
ample.x5tbsdemo;
import android.app.Activity;
t.ContentResolver;
t.Context;
t.Intent;
import android.database.Cursor;
import android.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import com.silang.superfileview.R;
t.smtt.sdk.TbsReaderView;
import java.io.File;
public class Main extends Activity {
private static final int CHOOSE_REQUEST_CODE = 1000;
private FrameLayout frameLayout;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
setContentView(R.layout.main);
frameLayout = (FrameLayout) findViewById(R.id.fl);
}
public void onClick(View view) {
switch (Id()) {
case R.id.button:
case R.id.button:
chooseFile();
break;
}
}
//选择⽂件
private void chooseFile() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
Intent chooser = ateChooser(intent, "请选择要代开的⽂件");
startActivityForResult(chooser, CHOOSE_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case CHOOSE_REQUEST_CODE:
Uri uri = Data();
String path = getPathFromUri(this, uri);
openFile(path);
break;
}
}
}
}
/**
* 打开⽂件
*/
private void openFile(String path) {
TbsReaderView readerView = new TbsReaderView(this, new TbsReaderView.ReaderCallback() { @Override
public void onCallBackAction(Integer integer, Object o, Object o1) {
}
});
//通过bundle把⽂件传给x5,打开的事情交由x5处理
Bundle bundle = new Bundle();
//传递⽂件路径
bundle.putString("filePath", path);
//加载插件保存的路径
bundle.putString("tempPath", ExternalStorageDirectory() + File.separator + "temp");
//加载⽂件前的初始化⼯作,加载⽀持不同格式的插件
boolean b = readerView.preOpen(getFileType(path), false);
if (b) {
readerView.openFile(bundle);
}
frameLayout.addView(readerView);
}
/***
* 获取⽂件类型
*
* @param path ⽂件路径
* @return⽂件的格式
*/
private String getFileType(String path) {
String str = "";
if (TextUtils.isEmpty(path)) {
return str;
return str;
}
int i = path.lastIndexOf('.');
if (i <= -1) {
return str;
}
str = path.substring(i + 1);
return str;
}
/**
* @param context
* @param uri    ⽂件的uri
* @return⽂件的路径
*/
public String getPathFromUri(final Context context, Uri uri) {
// 选择的图⽚路径
String selectPath = null;
final String scheme = Scheme();
if (uri != null && scheme != null) {
if (scheme.equals(ContentResolver.SCHEME_CONTENT)) {
// content://开头的uri
Cursor cursor = ContentResolver().query(uri, null, null, null, null);
if (cursor != null && veToFirst()) {
int columnIndex = ColumnIndexOrThrow(MediaStore.Images.Media.DATA);
// 取出⽂件路径
selectPath = String(columnIndex);
// Android 4.1 更改了SD的⽬录,sdcard映射到/storage/sdcard0
if (!selectPath.startsWith("/storage") && !selectPath.startsWith("/mnt")) {
// 检查是否有"/mnt"前缀
selectPath = "/mnt" + selectPath;
}
//关闭游标
cursor.close();
}
} else if (scheme.equals(ContentResolver.SCHEME_FILE)) {// file:///开头的uri
// 替换file://
selectPath = String().replace("file://", "");
int index = selectPath.indexOf("/sdcard");
selectPath = index == -1 ? selectPath : selectPath.substring(index);
if (!selectPath.startsWith("/mnt")) {
// 加上"/mnt"头
selectPath = "/mnt" + selectPath;
}
}
}
return selectPath;
}
}
清单⽂件要添加权限
安卓app开发用什么框架<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Note: Tbs不⽀持加载⽹络的⽂件,需要先把⽂件下载到本地,然后再加载出来