如何从.obb文件中提取文件?

时间:2015-07-13 22:06:06

标签: android zip expansion apk-expansion-files

在我的应用程序中,我在Android-> obb-> packagename-> main.1.packagename.obb下载了扩展文件。 有人可以向我解释,即使使用示例代码如何从中提取我的文件?

我尝试将APK Expansion Zip Library用作:

ZipResourceFile expansionFile = APKExpansionSupport.getAPKExpansionZipFile(MainActivity.this, 3, 0);  
for (ZipResourceFile.ZipEntryRO entry : expansionFile.getAllEntries())
Log.d("",entry.mFileName);
InputStream input = expansionFile.getInputStream(Environment.getExternalStorageDirectory().toString()
                            + "/sdcard/Android/obb/com.example.project/main.3.com.example.project.obb/obb/file.zip/file.apk");

但是expandFile总是为null。 .obb文件是使用Jobb创建的,用于文件夹obb / file.zip。

2 个答案:

答案 0 :(得分:1)

使用以下代码解决:

    final StorageManager storageManager = (StorageManager) getSystemService(STORAGE_SERVICE);
    String obbPath = Environment.getExternalStorageDirectory() + "/Android/obb";
    final String obbFilePath = obbPath + "/com.example.project/main.3.com.example.project.obb";
    OnObbStateChangeListener mount_listener = new OnObbStateChangeListener() {
          public void onObbStateChange(String path, int state) {
                if (state == OnObbStateChangeListener.MOUNTED) {
                      if (storageManager.isObbMounted(obbFilePath)) {
                            Log.d("Main","Mounted successful");
                            String newPath  = storageManager.getMountedObbPath(obbFilePath);
                            File expPath = new File(newPath+"/file.zip/file.apk");
                            Log.d("Main","File exist: " + expPath.exists());
                      }
                }
          }
    };
    storageManager.mountObb(obbFilePath, "key", mount_listener);

安装.obb文件后,我可以在路径mnt / obb / myfolder中访问我的数据。

答案 1 :(得分:0)

我用它来从obb中读取特定文件:

在AndroidManifest.xml中:

<!-- Needed for reading the obb file -->
<provider
    android:name=".extension.ZipFileContentProvider"
    android:authorities="com.example.extension.ZipFileContentProvider"/>

ZipFileContentProvider:

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

获取文件URI:

final Uri CONTENT_URI = Uri.parse("content://" + "com.example.extension.ZipFileContentProvider");
Uri uri = Uri.parse(CONTENT_URI + "/drawable/" + name)

要创建obb文件,我使用了gradle脚本(res文件夹位于同一目录中):

task createExpansionFile(type: Zip) {
  from 'res'

  entryCompression = ZipEntryCompression.STORED
  archiveName = 'main' + '.' + '1' + '.' + 'com.example' + '.obb'

  println archiveName
  println relativePath(destinationDir)
  println relativePath(archivePath)
}