What is the counterpart of iOS' QLPreviewContr

2019-04-20 02:34发布

问题:

On iOS it's pretty easy to preview many types of files by using QLPreviewController or UIDocumentInteractionController. How can something similar be achieved on Android and what file formats are supported?

回答1:

The bad news: there is no built-in (SDK level) preview component/View for you to use.

The good news: Android supports Intents that allow you to leverage other apps to do the work for you and return a selected file with a requested file type.

There are a few things that you can do depends on your targets:

  1. API 19+: On device with API 19, you should use the Storage Access Framework (https://developer.android.com/guide/topics/providers/document-provider.html) All the previews etc will be done on the responding app.

    You will be looking at using Intent.ACTION_OPEN_DOCUMENT.

  2. For older devices: There is an intent that you can use with optional file type and category you can use. This will open either a built-in file selector OR a third party app.

    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    startActivityForResult(intent, REQUEST_CODE);
    

    **Note you could create a Intent chooser here.

  3. For devices that do not have any app that can respond to the Intent: You can either bundle something prebuilt (https://code.google.com/p/android-file-dialog/) or depending on the file type, you can query the content database for media files and display them yourself with a nice UI.

Additional Reference on how to use Intents and how it works: http://developer.android.com/guide/components/intents-filters.html