Some time ago, there was a requirement in the project to realize the function of document preview. To realize this function on Android is much more complicated than on IOS. The following is a summary of my investigation for your reference:

First, WebView web page display

This method is similar to the implementation of ios. The Url address of the network file can be displayed by loading the WebView.

1, the Google Doc

Similar to iOS, Google also provides an online document parsing function. You only need to pass the Url address of a remote document to Google server in a fixed format, and then use WebView control to load the new Url address and display it. The WebView loads the Url in the following format:

Docs.google.com/gview?embed…

However, this method cannot be used in Korea because Google service is not available in Korea

Office Web 365

An online preview function of Office documents provided by a third-party company can realize the online web access of Microsoft, Adobe, AND WPS documents on mobile and PC terminals.

Fixed format link: ow365.cn/?i= your website ID&f…

Such as: ow365. Cn /? I = 1 & furl = h…

The charges are as follows:

3, yongzhong DCS document online preview

Similar to the services provided by Office Web 365, you can add the remote document domain name in the personal management center and combine the document address to form a new Url access address. Then you can achieve online preview on PC and mobile phone. The Url format is as follows:

http://api url +? K =(user Key corresponding to domain name)+ & url =(online document address)

The charges are as follows:

Conclusion: These two methods are completely dependent on third-party cloud services. The advantage is that they provide a URL address, which can meet the requirements of online document preview on both mobile and PC, and the integration is relatively simple.

2. Open the local application

If the mobile device has local applications that can open Office documents, you can use them to open the document you want to work on, but you must first download the remote document to the device’s local storage.

Intent Opens the file.

Intent intent = new Intent(ACTION_VIEW); intent.addCategory(CATEGORY_DEFAULT); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); Uri uri=getUriForFile(file); String mimeType=getMimeType(uri); intent.setDataAndType(uri, mimeType); StartActivity (intent.createchooser (Intent, "open "));Copy the code

Third party SDK

1. PDF Open Source Library:

  • AndroidPdfViewer, PdfiumAndroid: Two Android platform implementations based on PDFIum, support in-app preview of PDF documents, support animation, zooming, gesture and double click operations. Only PDF documents can be previewed and downloaded to the local PC.
  • MuPDF: a lightweight open source PDF, also supports ONLY PDF format documents, files need to be downloaded to the local.
  • Foxit PDF SDK: Foxit PDF SDK is a paid SDK from Foxit. It supports PDF display, navigation, creation, search, annotation, protection, PDF text extraction, image conversion, form data collection and editing, etc

Because these supported document formats only support PDF, so in fact, the background needs to be converted to PDF document format, and then android only preview the document in this format. For example, openOffice + JodConverter can be used in the background to convert Office documents to PDF format. This method requires openOffice to be installed on the server, and the conversion speed is slow for a large number of pages (within an acceptable range for a small number of pages, which has little to do with the size of the document, but more to do with the number of pages). Then the Android side can be used as AndroidPdfViewe third-party open source SDK preview these documents, preview time need to download to the local.

2. Use related technologies to complete document conversion

  • Server side conversion document: on the server side to convert office documents into HTML, PDF into pictures, Android side directly with webView load url can, the main look at the server this good implementation is not.
  • Complete document conversion on Android: use Poi to convert doc, DOCX, XLS, XLSX documents to HTML, then use WebView to load local HTML. Use Apache Poi components, but because PPT in the conversion process needs to use Java AWT, so can not achieve PPT conversion, that is to say, can only achieve word, EXCEL conversion.

3. Use Tencent Browsing Service (TBS)

Relying on the powerful capability of X5 kernel, you can realize the file browsing function and video playing function in the application. Directly integrate the SDK package provided by the official, integration can be achieved. This implementation still requires the documentation to be downloaded locally in advance.

Note: TBS relies on the X5 kernel. If there is no X5 kernel product (such as wechat, QQ, QQ browser) in the mobile phone, relevant documents cannot be loaded

Example code is as follows:

Import libraries:

implementation 'com.tencent.tbs.tbssdk:sdk:43903'
Copy the code
private void displayFile(File file) {
        Bundle bundle = new Bundle();
        bundle.putString("filePath", file.getAbsolutePath());

        File tempPath = new File(getExternalCacheDir().getAbsolutePath(),"tempPath/"+file.getName());

        if (!tempPath.getParentFile().exists()) {
            tempPath.mkdirs();
        }

        bundle.putString("tempPath", tempPath.getAbsolutePath());
        boolean result = mTbsReaderView.preOpen(getFileType(file.getAbsolutePath()), false);
        if (result) {
            mTbsReaderView.openFile(bundle);
        }
    }
Copy the code