1

On my Application I have lots of media file(.mp4) and pdf file so i have upload apk file with expansion .

but I have no idea how to play video file to Video view from expansion file(obb file) and open PDF file from expansion file .

anybody have idea about. please help me

Maikel Bollemeijer
  • 6,545
  • 5
  • 25
  • 48
Nirav Modh
  • 757
  • 1
  • 8
  • 18
  • Maby youll find here what you are looking for. http://stackoverflow.com/questions/14139587/android-expansion-file – Maikel Bollemeijer Apr 26 '13 at 12:32
  • Thnaks Maikel Bollemeijer ,But how to get Uri for palying video and open Pdf File ? – Nirav Modh Apr 26 '13 at 12:39
  • Just follow up how its pointed out on that post, but change the file extension. and for the playing or opening a video or pdf its just the same as how you would normaly do this. if you dont know how do a google search for this am sure you will get it to work. – Maikel Bollemeijer Apr 26 '13 at 12:43
  • Thnaks Maikel Bollemeijer,Google doc for expansion provide APEZProvider class for getting uri from expansion file . Have u idea about it how to use it .? – Nirav Modh Apr 26 '13 at 12:57
  • unfortunately I dont have any expiernce with using an expension file or the APEZProvider class, sorry man. – Maikel Bollemeijer Apr 26 '13 at 13:01

2 Answers2

1

You can use the APKExpansionSupport class to obtain reference to the expansion file and then open the asset from file.

// Get a ZipResourceFile representing a merger of both the main and patch files
try {  
    ZipResourceFile expansionFile = APKExpansionSupport.getAPKExpansionZipFile(this, 2, 0);
    AssetFileDescriptor afd = expansionFile.getAssetFileDescriptor("path-to-music-from-expansion.mp3");

    try {
        mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
    } catch (IllegalArgumentException | IllegalStateException | IOException e) {
        Log.w(TAG, "Failed to update data source for media player", e);
    }

    try {
        mMediaPlayer.prepareAsync();
    } catch (IllegalStateException e) {
        Log.w(TAG, "Failed to prepare media player", e);
    }
    mState = State.Preparing;

    try {
        afd.close();
    } catch (IOException e) {
        Log.d(TAG, "Failed to close asset file descriptor", e);
    }
} catch (IOException e) {
    Log.w(TAG, "Failed to find expansion file", e);
}

If you're using your expansion files to store media files, a ZIP file still allows you to use Android media playback calls that provide offset and length controls (such as MediaPlayer.setDataSource() and SoundPool.load()). In order for this to work, you must not perform additional compression on the media files when creating the ZIP packages. For example, when using the zip tool, you should use the -n option to specify the file suffixes that should not be compressed:

zip -n .mp4;.ogg main_expansion media_files

For more details and code for setting up expansion files, read How to Set Up Android App to Support Expansion Files.

Sapan Diwakar
  • 10,480
  • 6
  • 33
  • 43
0

Its actually quite easy to read movie files and other stuff directly from the expansion file using the zip tools that google has provided (com.android.vending.zipfile).

First get the expansionfile using the methods provided in the library, the paremeters are integers that represent your main expansion apk version (the apk version where the expansion pack you need was first added) and the patch apk version.

ZipResourceFile expansionFile = APKExpansionSupport.getAPKExpansionZipFile(context, APKX_MAIN_APK, APKX_PATCH_APK);

Video

For playing video directly from this zipresourcefile:

AssetFileDescriptor a = expansionFile.getAssetFileDescriptor(pathToFileInsideZip);

Now from this assetFileDescriptor you can get a FileDescriptor and use this in your mediaplayer, the correct syntax to get your mediaplayer to play the video also needs the second and third parameter.. Be it the startoffset and length you can get from the AssetFileDescriptor.

player.setDataSource(a.getFileDescriptor(), a.getStartOffset(), a.getLength());

Other

For all the other stuff (like images) you can just get an inputstream of the zipresourcefile:

expansionFile.getInputStream(pathToFileInsideZip);`

ALSO make sure you don't compress the videos in the zip for this to work! for example not to compress .mp4 files:

zip -n .mp4 -r zipfile.zip . -x ".*" -x "*/.*"
Jordy
  • 1,764
  • 1
  • 22
  • 32
  • I'm using that to get a pdf from the expansion file `expansionFile.getInputStream(path + pdfFilename);` but I get an infinite loop when I use that InputStream to copy to a file storage directory to read the pdf. What I'm doing wrong? I have the rest of the details here http://stackoverflow.com/questions/26606143/how-to-read-pdfs-from-expansion-file – tvieira Oct 28 '14 at 15:29