开发者

Android实现在线预览office文档的示例详解

目录
  • 一、项目概述
  • 二、相关技术知识
  • 三、实现思路
    • 3.1 方案一:WebView + Office Online
    • 3.2 方案二:TBS SDK 离线预览
  • 四、整合代码
    • 4.1 Java 代码(MainActivity.java)
    • 4.2 XML 布局与 Manifest
  • 五、代码解读
    • 六、项目总结
      • 七、实践建议与未来展望

        一、项目概述

        在移动端展示在线 Office 文档(如 Word、Excel、PPT)是一项常见需求。用户点击链接即可在 App 内预览,无需下载应用或文件到本地。实现该功能主要有两种思路:

        WebView + 在线文档服务:利用微软 Office Online 或 Google Docs Viewer,将文档 URL 嵌入预览页面;

        第三方 SDK(如腾讯 TBS/wpS):集成浏览器内核或文档 www.devze.comSDK,支持在本地加载远程文档并渲染显示。

        本文重点介绍两种方案的实现方法,并给出完整例子。

        二、相关技术知识

        1.WebView 加载在线预览

        Office Online URL 模板:

        https://view.officeapps.live.com/op/view.ASPx?src=<URL_ENCODED_DOC_URL>

        Google Docs Viewer:

        https://docs.google.com/gview?embedded=true&url=<URL_ENCODED_DOC_URL>

        WebView 配置:开启 javascript、支持缩放、调整缓存策略等。

        2.腾讯 TBS SDK(X5 内核)

        TBS 提供的 TbsReaderView 可加载 doc/docx/xls/ppt/pdf 等格式。

        需在 App 初始化时预加载内核,并在布局中添加 TbsReaderView。

        3.文件访问与权限

        在线预览不需读写权限;

        若下载到本地后用 SDK 打开,则需申请存储权限(android 6.0+ 运行时权限)。

        4.性能与兼容

        WebView 方案依赖外部网络和服务;

        SDK 方案包体较大但支持离线,可自定义 UI。

        三、实现思路

        3.1 方案一:WebView + Office Online

        在布局中放置一个 WebView。

        在 Activity 中取得文档远程 URL,进行 URL 编码后拼接到 Office Online 查看地址。

        配置 WebView:启用 JavaScript、DOM 存储、缩放控制。

        调用 webView.loadUrl(previewUrl) 即可在线预览。

        3.2 方案二:TBS SDK 离线预览

        在项目 build.gradle 中引入 TBS SDK 依赖。

        在 Application 启动时初始化 X5:调用 QBSDk.initX5Environment(...)。

        在布局中添加 TbsReaderView。

        在 Activity 中下载或缓存文档到本地,然后调用 tbsView.openFile(bundle, null) 加载预览。

        记得在 onDestroy() 中销毁 TbsReaderView。

        四、整合代码

        4.1 Java 代码(MainActivity.java)

        package com.example.officedocpreview;
         
        import android.Manifest;
        import android.content.pm.PackageManager;
        import android.net.Uri;
        import android.os.*;
        import android.view.View;
        import android.webkit.*;
        import android.widget.*;
        import androidx.annotation.NonNull;
        import androidx.appcompat.app.AppCompatActivity;
        import androidx.core.app.ActivityCompat;
        import androidx.core.content.ContextCompat;
        import com.tencent.smtt.sdk.TbsReaderView;
         
        import java.io.File;
        impjsort java.io.FileOutputStream;
        import java.io.InputStream;
        import java.net.HttpURLConnection;
        import java.net.URL;
         
        public class MainActivity extends AppCompatActivity {
         
            private static final int REQ_STORAGE = 1001;
            private WebView webView;
            private TbsReaderView tbsView;
            private FrameLayout container;
            private String remoteDocUrl = "https://example.com/test.docx";
         
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
         
                webView    = findViewById(R.id.web_view);
                tbsView    = findViewById(R.id.tbs_view);
                container  = findV编程客栈iewById(R.id.tbs_container);
         
                findViewById(R.id.btn_web_preview).setOnClickListener(v -> previewWithWeb());
                findViewById(R.id.btn_tbs_preview).setOnClickListener(v -> previewWithTbs());
            }
         
            /** 方案一:WebView + Office Online */
            private void previewWithWeb() {
                webView.setVisibility(View.VISIBLE);
                container.setVisibility(View.GONE);
         
                WebSettings ws = webView.getSettings();
                ws.setJavaScriptEnabled(true);
                ws.setBuiltInZoomControls(true);
                ws.setDisplayZoomControls(false);
                ws.setDomStorageEnabled(true);
         
                String urlEncoded = Uri.encode(remoteDocUrl);
                String previewUrl = "https://view.officeapps.live.com/op/view.aspx?src=" + urlEncoded;
                webView.loadUrl(previewUrl);
            }
         
            /** 方案二:TBS SDK 预览,需要存储权限 */
            private void previewWithTbs() {
                webView.setVisibility(View.GONE);
                container.setVisibility(View.VISIBLE);
                if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQ_STORAGE);
                } else {
                    downloadAndOpenWithTbs(remoteDocUrl);
                }
            }
         
            @Override
            public void onRequestPermissionsResult(int req,
                @NonNull String[] perms, @NonNull int[] grantResults) {
                if (req == REQ_STORAGE && grantResults.length>0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    downloadAndOpenWithTbs(remoteDocUrl);
                } else {
                    Toast.makeText(this, "存储权限被拒绝", Toast.LENGTH_SHORT).show();
                }
            }
         
            /** 下载到本地后用 TbsReaderView 打开 */
            private void downloadAndOpenWithTbs(String urlStr) {
                new Thread(() -> {
                    try {
                        URL url = new URL(urlStr);
                        HttpURLConnection conn =
                            (HttpURLConnection) url.openConnection();
                        conn.connect();
                        InputStream is = conn.getInputStream();
                        File file = new File(getExternalFilesDir(null), "temp.docx");
                        FileOutputStream fos = new FileOutputStream(file);
                        byte[] buf = new byte[4096];
                        int len;
                        while ((len = is.read(buf))>0) fos.write(buf,0,len);
                        fos.close();
                        isphp.close();
         
                        runOnUiThread(() -> openFileInTbs(file.getAbsolutePath()));
                    } catch (Exception e) {
                        e.printStackTrace();
                        runOnUiThread(() ->
                            Toast.makeText(this, "下载失败", Toast.LENGTH_SHORT).show());
                    }
                }).start();
            }
         
            private void openFileInTbs(String filePath) {
                Bundle params = new Bundle();
                params.putString(TbsReaderView.KEY_FILE_PATH, filePath);
                params.putString(TbsReaderView.KEY_TEMP_PATH,
                    getExternalFilesDir(null).getPath());
                boolean ok = tbsView.preOpen(getFileType(filePath), false);
                if (ok) {
                    tbsView.openFile(params);
                } else {
                    Toast.makeText(this, "TBS 内核未加载或不支持", Toast.LENGTH_SHORT).show();
                }
            }
         
            private String getFileType(String path) {
                if (path == null || !path.contains(".")) return "";
                return path.substring(path.lastIndexOf('.') + 1);
            }
         
           编程客栈 @Override
            protected void onDestroy() {
                super.onDestroy();
                tbsView.onStop();
            }
        }

        4.2 XML 布局与 Manifest

        <!-- AndroidManifest.xml —— 声明 INTERNET 与读写权限,并注册 TbsReaderView 权限 -->
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
            package="com.example.officedocpreview">
          <uses-permission android:name="android.permission.INTERNET"/>
          <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
          <application
              android:allowBackup="true"
              android:label="OfficePreview"
              android:icon="@mipmap/ic_launcher"
              android:theme="@style/Theme.AppCompat.Light.NoActionBar">
            <activity android:name=".MainActivity">
              <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
              </intent-filter>
            </activity>
          </application>
        </manifest>
        
        ​​​​​​​<!-- activity_main.xml —— 同时包含 WebView 与 TbsReaderView -->
        <?xml version="1.0" encoding="utf-8"?>
        <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
         
          <!-- WebView 方式预览 -->
          <WebView
              android:id="@+id/web_view"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:visibility="gone"/>
         
          <!-- TBS SDK 方式预览 -->
          <FrameLayout
              android:id="@+id/tbs_container"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:visibility="gone">
            <com.tencent.smtt.sdk.TbsReaderView
                android:id="@+id/tbs_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
          </FrameLayout>
         
          <!-- 底部按钮 -->
          <LinearLayout
              android:layout_gravity="bottom|center_horizontal"
              android:orientation="horizontal"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="16dp">
            <Button
                android:id="@+id/btn_web_preview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Web 预览"/>
            <Button
                android:id="@+id/btn_tbs_preview"
                android:layout_marginStart="16dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TBS 预览"/>
          </LinearLayout>
        </FrameLayout>

        五、代码解读

        1.WebView 方案

        将文档 URL 进行 URL 编码后拼接到 Office Online 查看地址;

        在 WebView 中启用 JavaScript、缩放支持及 DOM 存储;

        直接 loadUrl() 即可,无需额外下载。

        2.TBS SDK 方案

        需提前在 Application 或任意时机调用 QbSdk.initX5Environment()(省略);

        下载文档到 App 私有存储后,通过 TbsReaderView.preOpen() 检测内核支持;

        调用 openFile() 加载本地文档,支持 doc、xls、ppt、pdf 等多种格式;

        在 onDestroy() 中调用 tbsView.onStop() 释放资源。

        3.权限与生命周期

        运行时申请写存储权限后才能保存到本地;

        在 onPause()/onDestroy() 清理动画和 TBS 资源,避免内存泄漏。

        六、项目总结

        WebView + Office Online:实现简单、无需集成第三方 SDK,依赖外部服务;

        TBS SDK:支持离线预览和更多格式,但包体较大、需初始化内核;

        本文示例将两种方案合二为一,按需选择并切换。

        七、实践建议与未来展望

        UI 优化:增加加载进度条、错误页面提示;

        缓存策略:对已下载文件做缓存,下次直接读取;

        更多格式:结合 open-source 渲染库(如 Apache POI + PDF 转换)实现更多定制;

        Compose 时代:在 Jetpack Compose 中也可直接使用 AndroidView 嵌入 WebView 或 TBS。

        到此这篇关于Android实现在线预览office文档的示例详解的文章就介绍到这了,更多相关Android在线预览office内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!

        0

        上一篇:

        下一篇:

        精彩评论

        暂无评论...
        验证码 换一张
        取 消

        最新开发

        开发排行榜