Different from iOS, Android webView does not support opening Office and PDF documents, so when we encounter the need to open Office and PDF documents in the app, we often cannot support it from the system’s native functions. This article is about integrating Office and PDF files into Android applications. The demo address is github.com/windinwork/…

First, determine the solution

Android apps open Office and PDF files. The following four solutions are commonly used:

  1. Open files from online web pages: Open Office and PDF files from online pages provided by Microsoft or Google
  2. Integrate related document handling open source libraries: By integrating open source libraries similar to AndroidPdfViewer
  3. Open documents through third-party applications in the system
  4. Integrate Tencent X5 SDK file capabilities

Each of the four schemes has its advantages and disadvantages. Here, the author chooses X5 SDK as the main means and such a solution assisted by third-party applications

Second, integrate x5 kernel

There are two versions of x5 kernel officially provided by Tencent. Here we choose the SDK with file function and function:

The following integration can be referred to the X5 kernel access documentation, which is not detailed here. The main task of integration is to integrate the JAR package with the so file and call qbsdK.initx5Environment (Context, callback) during Application initialization to complete the initialization.

TbsReader integration

The X5 kernel provides the TbsReaderView, which allows us to display documents in our App through this class. Since the TbsReaderView class has a lifecycle method, we wrapped it in a Fragment for easy calls. TbsReaderView has two main methods: preOpen(String, Boolean) and openFile(Bundle). PreOpen (String, Boolean) checks whether x5 file capabilities have been successfully initialized and whether the open file format is supported. Returns true if the open file condition is met. OpenFile (Bundle) is called when preOpen(String, Boolean) returns true. As the name implies, this method is used to open the file, where the Bundle is passed in the file path.

String path = file.getPath();
String format = parseFormat(path);
boolean preOpen = mReaderView.preOpen(format, false); // This status indicates whether the x5 file capability has been successfully initialized and supports the open file formatif(preOpen) {// Open office file Bundle Bundle = new Bundle(); bundle.putString("filePath", path);
    bundle.putString("tempPath", StorageUtils.getTempDir(context).getPath());
    mReaderView.openFile(bundle);
}
Copy the code

With this core code, TbsReaderView basically opens Office and PDF files.

Fourth, improve the document ability

There are all kinds of Android phones on the market, and even though they integrate WITH TbsReaderView, they still get feedback that they can’t open Office files. This is because the X5 file capability on the user’s phone did not initialize successfully, and it is not clear why. For these users, we need to provide an alternative way to open Office files when they cannot use TbsReaderView to view them. If you detect that TbsReaderView cannot open Office or PDF, jump to a third-party application to open it. OpenFileReader (Context, String, HashMap<String, String>, ValueCallback) is used to open files using third-party applications. And support to download QQ browser with Office browsing function, such a function is more user-friendly, we can directly use.

However, x5 jar packages open with third-party applications that call uri.fromfile (file). This method of generating file uris works under Android7.0, but will crash on Android7.0 and above due to file permissions. To enable Android7.0 and above users to jump to third-party applications, we need to use the FileProvider to get the Uri, but the code is dead in the Jar package. Fortunately, after many attempts, it was possible to copy the part of the code that jumped to the opening of a third-party application and fix the uri.fromfile (file) code to call properly without having to modify the JAR. Here I’ve wrapped this part of the code in a class called TbsReaderAssist to aid the call.

In this way, a relatively complete function to open Office and PDF is done.

String path = file.getPath();
String format = parseFormat(path);
boolean preOpen = mReaderView.preOpen(format, false); // This status indicates whether the x5 file capability has been successfully initialized and supports the open file formatif(preOpen) {// Open office file Bundle Bundle = new Bundle(); bundle.putString("filePath", path);
    bundle.putString("tempPath", StorageUtils.getTempDir(context).getPath());
    mReaderView.openFile(bundle);
} else{// Failed to open file, possibly due to unsuccessful initialization of the X5 kernelif(qbsdk.issuportOpenFile (format, 1)) {// Check whether the file supports HashMap<String, String> params = new HashMap<>(); params.put("style"."1");
        params.put("local"."false"); TbsReaderAssist.openFileReader(context, path, params, null); }}Copy the code

Five, the summary

Here, the author wrote a solution for opening Office or PDF files by App, which I think is relatively perfect for an App. Here is the demo address: github.com/windinwork/… , shared out, can let have the need to do similar functions of small partners to take some detours.