Android preview Office documents

Preview Office documents such as doc, pdf, etc. on Android. I think most of the Android development partners will meet such needs. iOS has the system ability to support online opening, and only one link is needed, while Android wants to achieve It is very difficult to open online!

Android realizes opening PDF online

The Android system itself does not provide the ability to open documents online. If you want to realize it, you need to find a way by yourself, implement it by your own technical team, or use existing solutions on the Internet such as pdfjs . pdfjs is a web project solution for opening pdf online implemented by mozilla. , after starting the project, you only need to add your pdf link after the url, and you can open it online. After opening it on the PC side, it will look like this:
insert image description here

pdfjs can be used as a functional module, embedded in the original backend project, the project structure of pdfjs:

insert image description here
When using, just load the link in the browser: http://xxx/pdf/web/viewer?filepath=http://xxx.pdf

pdfjs:

  • Advantages: Easy to deploy and easy to use!
  • Disadvantages:
    The loading efficiency of using webview on the mobile terminal is average, and it is likely to be slow to load;
    only pdf documents can be loaded, and the loading of documents in other formats is not supported;

Based on the above two shortcomings, when I use it in actual projects, I am often complained by the product and asked for optimization, but this is not a problem that can be solved by one person! Because there is indeed a need to view various documents on the mobile phone in the project, it can only be realized by replacing other solutions!

Tencent TBS

Tencent's tbs service does provide a lot of convenience for mobile developers, such as a more powerful and unified webview loading experience, and the ability to open various documents!

tbs document address: https://x5.tencent.com/tbs/product/file.html

What needs to be pointed out here is that tbs does not support the online opening function , that is to say, the tbs sdk you introduced can only open local documents, so in specific use, you can only download the documents you want to open to the local, You can only use the tbs document ability to open on the mobile phone (some third-party tools on the Android side, such as pdfview , need to be downloaded first and then opened), you may find it troublesome, but this should be the best solution for the current mobile document opening ability Yes, unless your business only needs to open pdf documents and can accept the shortcomings of pdfjs, then you can use pdfjs to open online!

In this blog, I will share the solution in my project with you, I hope it will be helpful to everyone!

TBS+AgentWeb+pdfjs+system capability

AgentWeb: a third-party tool library https://github.com/Justson/AgentWeb , which realizes the packaging of native webview, simplifies some commonly used functions, and provides file selection and download capabilities!

The TbsReaderView contained in TBS is the key class of document capability!

The combination of the three uses the download capability of AgentWeb. When the url is found to be a file download link, the file is automatically downloaded and the progress is displayed on the front end. After the download is successful, TbsReaderView is automatically called to load the document. If it is a pdf document, call pdfjs to open it online; if it is another document, call the system software to open it!

Project structure:

insert image description here

The effect is as follows, the first start:

insert image description here

Because tbs initialization needs to download the tbs kernel, so that your application can be successfully uploaded to the application market, try to perform tbs initialization after the user agrees to the agreement:

public class App extends Application {
    
    

    private static App INSTANCE;

    public static App getContext() {
    
    
        return INSTANCE;
    }

    @Override
    public void onCreate() {
    
    
        super.onCreate();
        INSTANCE = this;
        if (SysUtils.isAppMainProcess()) {
    
    
            initTBS();
        }
    }

    private void initTBS() {
    
    
        if (!SPUtils.getBooleanDefaultFalse("agreement")) return;//未同意隐私协议前不进行初始化
        TBSUtil.init();
    }

}

Of course you will ask, isn't tbs loaded on the first startup? of course not! Let’s talk about a pitfall of tbs here, that is, if you only rely on the documents provided by tbs for initialization, there is a high probability that the first load of the tbs kernel will fail, so we need to actively download and load the kernel, and in order to ensure The kernel can be downloaded successfully, and I executed the initialization of tbs again in MainActivity:

public class MainActivity extends AppCompatActivity {
    
    

    ActivityMainBinding vb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        vb = ActivityMainBinding.inflate(LayoutInflater.from(this));
        setContentView(vb.getRoot());
        TBSUtil.init();
        vb.btnOpen.setOnClickListener(view -> {
    
    
            if (TextUtils.isEmpty(vb.etContent.getText().toString())) return;
            if (!QbSdk.isTbsCoreInited()) {
    
    
                Toast.makeText(this, "TBS内核未加载成功,请稍后...", Toast.LENGTH_LONG).show();
                return;
            }
            Intent intent = new Intent(this, OfficeWebActivity.class);
            intent.putExtra(OfficeWebActivity.PARAM_URL, vb.etContent.getText().toString());
            startActivity(intent);
        });
    }
}

When installing for the first time, the application does not perform initialization, but agrees to enter the MainActivity after the agreement is initialized. When it is opened next time, the application initialization operation will work. At the same time, it must be judged in the tbs loading callback. If loaded If unsuccessful, take the initiative to download the tbs kernel and load it !

Because the functions in this project are simple, you can click the open document button directly after starting the program, but at this time the tbs core has not been loaded successfully, you can use QbSdk.isTbsCoreInited() to judge, only after the TBS core is successfully loaded can you use TbsReaderView !

I printed out the log of kernel download and loading:

insert image description here

The whole process takes about 20 seconds, so the tbs document opening function cannot be used during this period, but considering that in normal usage scenarios, users cannot immediately find a certain document to open and view it when entering the app, and may have to go through login registration during this period. In the process of searching for documents, generally, this time is enough to complete the download of the tbs kernel. If there are special circumstances, please give a waiting reminder!

Loading pdf document effect:

insert image description here

Of course, the effect of loading other tbs-supported documents is the same, they are all downloaded first and then loaded!

Once the first loading is successful, that is, the document has been downloaded to the local, then it will be opened directly when entering again:

insert image description here

Of course, there are also document file processing methods that tbs does not support. Here is an apk file link tested:

insert image description here

Project download address:

csdn:https://download.csdn.net/download/baiyuliang2013/85040292

Note: At present, the document preview capability of tbs is operated (harvested) by Tencent as a separate charging module, so if you want to continue to use this function, you must either pay for it or integrate the offline kernel (you can search for the old version of the kernel on Baidu). More than 40 M. In this way, in order to use the pdf preview function, the app will need more than 40 M, so this needs to be considered by yourself. Another way is to use the open source Android pdfViewer, and the last way is to integrate pdfjs at the back end for online preview.

Guess you like

Origin blog.csdn.net/baiyuliang2013/article/details/123755135