Is there a way to get a Url to a specific file in Android expansion file?

ぐ巨炮叔叔 提交于 2019-12-12 17:19:46

问题


I am building a PhoneGap application, which contains large audio and video files. In Android the media files should be in an expansion file to keep the application size under the Google Play 50 MB limit.

I'm currently considering two ways to play the video files:

  1. Unpack (maybe temporarily) the desired video file from expansion file to sdcard, get its URL and start the media player application. Unpacking takes some time so the user would have to wait for a few seconds depending on the size of the video.
  2. Write my own video player as PhoneGap plugin using Android's VideoView class. This way I can play directly from the expansion file without first unpacking the file and get more responsive application.

However, I wonder if there is a way to get a URL to the video files inside the expansion file? That would allow my application to add the URL to the Intent with which the user's preferred media player application is started and I would prefer this approach.


回答1:


I found myself a way to do this through a content provider. I got a hint about the Content Provider from question android play movie inside expansion file, although in that question the URL was not used to start an external application.

We need a ContentProvider class that extends Google's APEZProvider:

public class ZipFileContentProvider extends APEZProvider {
    @Override
    public String getAuthority() {
        return "com.example.expansionexperiment.provider";
    }
}

And it needs to be added to Manifest and exported:

<provider
    android:exported="true"
    android:name="com.example.expansionexperiment.ZipFileContentProvider"
    android:authorities="com.example.expansionexperiment.provider" />

The authority in the manifest and in getAuthority() must be the same.

Now we can create a Uri to the file inside Expansion File:

// Filename inside my expansion file
String filename = "video/video.mp4";
String uriString = "content://com.example.expansionexperiment.provider" +  File.separator + filename;
Uri uri = Uri.parse(uriString);

// Start video view intent
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "video/*");
startActivity(intent);

The content provider provides now access to the zip file and the files within just with the URL.

However, PhoneGap's Media plugin does not support content providers, because if the URL does not start with http, it inserts "/sdcard/" in front of the URL. (!!!) Therefore, to play audio files through the zip content provider, you will need to write your own audio plugin.



来源:https://stackoverflow.com/questions/16831273/is-there-a-way-to-get-a-url-to-a-specific-file-in-android-expansion-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!