使用Android NDK无法读取OBB文件内容

时间:2013-08-08 22:08:58

标签: android android-ndk

我使用OBB来保存我的数据,需要使用Android NDK来访问数据。但是,我似乎无法挂载obb文件来检索数据。根据{{​​3}},我根据计划命名我的obb。

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

然后我将其放在/data/Android/obb/com.example.native_activity文件夹下,如上面的在线文档所示。

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

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打印相矛盾,该调用表明它尚未挂载。

我完全糊涂了。

1 个答案:

答案 0 :(得分:5)

自己解决了。

对于mount-query API肯定存在反直觉的问题,因为当我检查文件系统时,obb已经挂载在/ mnt / obb /下,但是API一直返回0并且拒绝给我路径。它可能(详细语义没有​​记录在任何地方)意味着 THIS 非常请求未能安装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. 使用jobb创建OBB时,除了正确命名文件外,还必须使用-pv选项与清单中正确的versionCode一起使用。
  2. 每当需要对同一OBB进行更新时,例如,OBB文件名中的versionCode保持不变但内容已更改,则需要首先明确删除设备上已有的OBB。否则,简单的adb-push OBB将无法工作。您将在mount回调中不断获得AOBB_STATE_ERROR_COULD_NOT_MOUNT(错误代码21)。这鼓励了版本化的使用,但是对同一个OBB进行了特别的测试,即,不建议在不增加versionCode的情况下继续更改OBB。这意味着您需要首先删除OBB以“覆盖”完全相同的OBB文件。
  3. 许多教程都专注于教你如何使用下载库以及从GooglePlay上传/下载舞蹈。如果你想做的就是在本地测试OBB访问,那完全可以理解。