Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the counterpart of iOS' QLPreviewController or UIDocumentInteractionController on Android?

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?

like image 931
Krumelur Avatar asked Dec 02 '13 14:12

Krumelur


1 Answers

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

like image 187
Edison Avatar answered Nov 06 '22 00:11

Edison