我使用 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 打印相矛盾,后者读取它尚未安装。
我完全糊涂了。