6

我使用 OBB 来保存我的数据,并且需要使用 Android NDK 来访问数据。但是,我似乎无法挂载 obb 文件来检索数据。我根据APK 扩展文件文档以方案命名我的 obb 。

例如,main.1.com.example.native_activity.obb

然后我按照上面在线文档的建议将它放在 /data/Android/obb/com.example.native_activity 文件夹下。

但是,通过使用以下代码,我无法进行 obb 挂载:

AStorageManager* man = AStorageManager_new();
char* data = malloc(256);
AStorageManager_mountObb(man, "main.1.com.example.native_activity.obb", "somekey", my_obbCallbackFunc, data);
char* obbPath = AStorageManager_getMountedObbPath(man, "main.1.com.example.native_activity.obb");

LOGI("mounted path: %s", obbPath);
free(data);
data = NULL;
AStorageManager_delete(man);
man = NULL;

结果obbPath总是空的。我不知道如何获得正确的密钥,所以它只是一个随机化。

更新

我从我这边纠正了两个问题。

首先,obb 文件不是使用 Android jobb 工具创建的,而是从 zip 文件重命名的。所以我使用以下方法重新创建了文件:

jobb -d assets/ -o obb/main.1.com.example.native_activity.obb -k mykey -pn com.example.native_activity -pv 11

把它推到下面

/sdcard/Android/obb/com.example.native_activity/

然后我在 mountObb 调用中使用了键和回调函数,代码如下:

char obbPath[256];
sprintf(obbPath, "/sdcard/Android/obb/com.example.native_activity/main.1.com.example.native_activity.obb");
struct stat sts;
if(stat(obbPath, &sts) == -1)
{
    LOGI("File not found: %s\n", obbPath);
}
else
{
    LOGI("File found: %s", obbPath);
}

AStorageManager* man = AStorageManager_new();
char* data = malloc(256);
AStorageManager_mountObb(man, obbPath, "mykey", my_obbCallbackFunc, data);
char* mntPath = AStorageManager_getMountedObbPath(man, obbPath);

int isMounted = AStorageManager_isObbMounted(man, obbPath);

LOGI("mounted path: %s, already mounted?: %d", mntPath, isMounted);
free(data);
data = NULL;
AStorageManager_delete(man);
man = NULL;
return 1;

和回调

void my_obbCallbackFunc(const char* filename, const int32_t state, void* data)
{
    LOGI("my_obbCallbackFunc: %d", state);
}

我得到的 Logcat 输出:

08-09 08:45:15.960: I/native-activity(9166): Touched screen.
08-09 08:45:15.960: I/native-activity(9166): File found: /sdcard/Android/obb/com.example.native_activity/main.1.com.example.native_activity.obb
08-09 08:45:15.960: E/Parcel(9166): Reading a NULL string not supported here.
08-09 08:45:15.960: I/native-activity(9166): mounted path: , already mounted?: 0
08-09 08:45:15.970: I/native-activity(9166): Touched screen.
08-09 08:45:15.970: I/native-activity(9166): File found: /sdcard/Android/obb/com.example.native_activity/main.1.com.example.native_activity.obb
08-09 08:45:15.970: E/Parcel(9166): Reading a NULL string not supported here.
08-09 08:45:15.970: I/native-activity(9166): mounted path: , already mounted?: 0
08-09 08:45:16.030: I/native-activity(9166): my_obbCallbackFunc: 1
08-09 08:45:16.030: I/native-activity(9166): my_obbCallbackFunc: 24
08-09 08:45:16.030: I/native-activity(9166): my_obbCallbackFunc: 24
08-09 08:45:16.030: I/native-activity(9166): my_obbCallbackFunc: 24
08-09 08:45:16.030: I/native-activity(9166): my_obbCallbackFunc: 24
08-09 08:45:16.030: I/native-activity(9166): my_obbCallbackFunc: 24
08-09 08:45:16.030: I/native-activity(9166): my_obbCallbackFunc: 24

根据android-ndk-r8d/platforms/android-9/arch-arm/usr/include/android/storage_manager.h,来自回调的错误代码意味着

AOBB_STATE_MOUNTED = 1,
AOBB_STATE_ERROR_ALREADY_MOUNTED = 24,

但是,这与 AStorageManager_isObbMounted() 调用的返回值的 logcat 打印相矛盾,后者读取它尚未安装。

我完全糊涂了。

4

1 回答 1

6

自己解决了。

mount-query API 肯定有一些违反直觉的东西,因为当我检查文件系统时 obb 已安装在 /mnt/obb/ 下,但 API 一直返回 0 并拒绝给我路径。它可能(细节语义没有记录在任何地方)意味着这个请求未能安装 obb,尽管该文件之前确实已经安装(来自回调的错误代码 24)。

因此,如果我在代码中再次安装之前先卸载它应该没问题。所以基本上添加这个:

AStorageManager_unmountObb(man, obbPath, 1, my_obbCallbackFunc, data);

AStorageManager_mountObb(man, obbPath, "mykey", my_obbCallbackFunc, data);

并带有回调:

void my_obbCallbackFunc(const char* filename, const int32_t state, void* data)
{
    LOGI("my_obbCallbackFunc: %d", state);

    AStorageManager* man = AStorageManager_new();
    int isMounted = AStorageManager_isObbMounted(man, filename);
    char* mntPath = AStorageManager_getMountedObbPath(man, filename);

    LOGI("my_obbCallbackFunc: fn: %s: mounted path: %s, already mounted?: %d", filename, mntPath, isMounted);
    AStorageManager_delete(man);
}

成功的输出:

08-09 10:41:53.060: I/native-activity(10753): File found: /sdcard/Android/obb/com.example.native_activity/main.1.com.example.native_activity.obb
08-09 10:41:53.070: E/Parcel(10753): Reading a NULL string not supported here.
08-09 10:41:53.070: I/native-activity(10753): mounted path: , already mounted?: 0
08-09 10:41:53.080: I/native-activity(10753): Touched screen.
08-09 10:41:53.080: I/native-activity(10753): File found: /sdcard/Android/obb/com.example.native_activity/main.1.com.example.native_activity.obb
08-09 10:41:53.110: I/native-activity(10753): my_obbCallbackFunc: 2
08-09 10:41:53.110: E/Parcel(10753): Reading a NULL string not supported here.
08-09 10:41:53.110: I/native-activity(10753): mounted path: , already mounted?: 0
08-09 10:41:53.110: E/Parcel(10753): Reading a NULL string not supported here.
08-09 10:41:53.110: I/native-activity(10753): my_obbCallbackFunc: fn: /storage/Android/obb/com.example.native_activity/main.1.com.example.native_activity.obb: mounted path: , already mounted?: 0
08-09 10:41:53.110: I/native-activity(10753): Touched screen.
08-09 10:41:53.110: I/native-activity(10753): File found: /sdcard/Android/obb/com.example.native_activity/main.1.com.example.native_activity.obb
08-09 10:41:53.110: E/Parcel(10753): Reading a NULL string not supported here.
08-09 10:41:53.110: I/native-activity(10753): mounted path: , already mounted?: 0
08-09 10:41:53.130: I/native-activity(10753): Touched screen.
08-09 10:41:53.130: I/native-activity(10753): File found: /sdcard/Android/obb/com.example.native_activity/main.1.com.example.native_activity.obb
08-09 10:41:53.130: E/Parcel(10753): Reading a NULL string not supported here.
08-09 10:41:53.130: I/native-activity(10753): mounted path: , already mounted?: 0
08-09 10:41:53.260: I/native-activity(10753): my_obbCallbackFunc: 1
08-09 10:41:53.300: I/native-activity(10753): my_obbCallbackFunc: 2
08-09 10:41:53.300: E/Parcel(10753): Reading a NULL string not supported here.
08-09 10:41:53.300: I/native-activity(10753): my_obbCallbackFunc: fn: /storage/Android/obb/com.example.native_activity/main.1.com.example.native_activity.obb: mounted path: , already mounted?: 1
08-09 10:41:53.300: E/Parcel(10753): Reading a NULL string not supported here.
08-09 10:41:53.300: I/native-activity(10753): my_obbCallbackFunc: fn: /storage/Android/obb/com.example.native_activity/main.1.com.example.native_activity.obb: mounted path: , already mounted?: 0
08-09 10:41:53.490: I/native-activity(10753): my_obbCallbackFunc: 1
08-09 10:41:53.520: I/native-activity(10753): my_obbCallbackFunc: 2
08-09 10:41:53.520: E/Parcel(10753): Reading a NULL string not supported here.
08-09 10:41:53.520: I/native-activity(10753): my_obbCallbackFunc: fn: /storage/Android/obb/com.example.native_activity/main.1.com.example.native_activity.obb: mounted path: , already mounted?: 0
08-09 10:41:53.520: E/Parcel(10753): Reading a NULL string not supported here.
08-09 10:41:53.520: I/native-activity(10753): my_obbCallbackFunc: fn: /storage/Android/obb/com.example.native_activity/main.1.com.example.native_activity.obb: mounted path: , already mounted?: 1
08-09 10:41:53.680: I/native-activity(10753): my_obbCallbackFunc: 1
08-09 10:41:53.720: I/native-activity(10753): my_obbCallbackFunc: 2
08-09 10:41:53.720: E/Parcel(10753): Reading a NULL string not supported here.
08-09 10:41:53.720: I/native-activity(10753): my_obbCallbackFunc: fn: /storage/Android/obb/com.example.native_activity/main.1.com.example.native_activity.obb: mounted path: , already mounted?: 1
08-09 10:41:53.720: E/Parcel(10753): Reading a NULL string not supported here.
08-09 10:41:53.720: I/native-activity(10753): my_obbCallbackFunc: fn: /storage/Android/obb/com.example.native_activity/main.1.com.example.native_activity.obb: mounted path: , already mounted?: 0
08-09 10:41:53.870: I/native-activity(10753): my_obbCallbackFunc: 1
08-09 10:41:53.880: I/native-activity(10753): my_obbCallbackFunc: fn: /storage/Android/obb/com.example.native_activity/main.1.com.example.native_activity.obb: mounted path: /mnt/obb/437f5d6d13a1da1d3b41bb46963e3720, already mounted?: 1

更新:在让它完全工作之前,我必须解决几个问题:

  1. 使用 创建 OBBjobb时,除了正确命名文件外,该-pv选项还必须与清单中的正确 versionCode 一起使用。
  2. 每当需要对同一个OBB进行更新时,例如,OBB文件名中的versionCode保持不变但内容发生了变化,则需要首先明确删除设备上已经存在的OBB。否则,简单的 adb-pushed OBB 将无法工作。您将AOBB_STATE_ERROR_COULD_NOT_MOUNT在挂载回调中不断收到(错误代码 21)。这鼓励了版本化的使用,但使对同一 OBB 的临时测试变得困难,即,不“建议”在不增加 versionCode 的情况下不断更改 OBB。这意味着您需要先删除 OBB 以“覆盖”完全相同的 OBB 文件。
  3. 许多教程侧重于教您如何使用下载库以及向/从 GooglePlay 上传/下载舞蹈。如果您只想在本地测试 OBB 访问,那是完全可以忽略的。
于 2013-08-09T17:43:35.973 回答