2013-04-26 87 views
1

在我的应用程序中,我有很多媒体文件(.mp4)和pdf文件,所以我已经上传apk文件与扩展。Android扩展文件。

但我不知道如何播放视频文件到视频视图从扩展文件(obb文件),并从扩展文件打开PDF文件。

有人有想法。 请帮我

+0

Maby你会发现这里你在找什么。 http://stackoverflow.com/questions/14139587/android-expansion-file – 2013-04-26 12:32:48

+0

Thnaks Maikel Bollemeijer,但如何让Uri获取视频并打开Pdf文件? – 2013-04-26 12:39:26

+0

只需跟进其在帖子中指出的方式,但更改文件扩展名即可。并播放或打开一个视频或PDF格式,就像你会如何正常操作一样。如果你不知道如何谷歌搜索这个肯定你会得到它的工作。 – 2013-04-26 12:43:48

回答

0

它实际上很容易直接从扩展文件中使用谷歌提供的zip工具(com.android.vending.zipfile)来阅读电影文件和其他东西。

首先使用库中提供的方法获取扩展文件,参数是代表您的主扩展apk版本(您首先需要的扩展包的apk版本)和修补程序apk版本的整数。

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

视频

直接从这个zipresourcefile播放视频:从这个assetFileDescriptor

AssetFileDescriptor a = expansionFile.getAssetFileDescriptor(pathToFileInsideZip); 

现在,你可以得到FileDescriptor的和您的媒体播放器使用,正确的语法来获得您的媒体播放器播放视频也需要第二个和第三个参数。不管它是从AssetFileDescriptor中获得的起始偏移量和长度。

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

其他

对于所有其他的东西(如图片)你可以得到的zipresourcefile的一个InputStream:

expansionFile.getInputStream(pathToFileInsideZip);` 

另外,还要确保你不压缩视频在这个zip的工作! 例如不压缩.mp4文件:

zip -n .mp4 -r zipfile.zip . -x ".*" -x "*/.*" 
+0

我使用它从扩展文件'expansionFile.getInputStream(path + pdfFilename)'获得pdf;'但是当我使用该InputStream复制到文件存储目录来阅读pdf时,我得到一个无限循环。我做错了什么?我有这里的其余细节http://stackoverflow.com/questions/26606143/how-to-read-pdfs-from-expansion-file – tvieira 2014-10-28 15:29:31

1

可以使用APKExpansionSupport类来获取参考扩展文件,然后从文件中打开该资产。

// 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); 
} 

如果您使用您的扩展文件存储多媒体文件,ZIP文件还允许您使用提供偏移Android的媒体播放和通话长度控件(如MediaPlayer.setDataSource()SoundPool.load())。为了达到此目的,在创建ZIP包时,您不得对媒体文件执行额外的压缩。例如,使用zip工具时,你应该使用-n选项指定不应被压缩的文件后缀:

zip -n .mp4;.ogg main_expansion media_files 

有关详细信息和代码设置扩展文件,阅读How to Set Up Android App to Support Expansion Files