0

我成功地将所需的 json 文件添加到我的应用程序的内部存储中data/<packagename>/files/。我想要实现的是,在我的 android 插件(在 Unity 应用程序内)中创建一个可以从内部存储读取相同 json 的函数。我怀疑,如果这在运行时甚至是可能的。我在我的插件 contentprovider 类中尝试了这个,String file_name=context.getFilesDir() + "/"+"stickers.json";但我的应用程序在设备上崩溃了。

我认为的第二种选择是使用 UnitySendMessage 将整个 json 对象传递给 Android 插件(我以前从未使用过它)。

我希望插件读取内部存储的 json,因为我可以在运行时从统一动态修改 json,并从 android 插件代码执行我想要的功能。

更新

--------- beginning of crash 2020-05-30 14:09:32.275 22270-22270/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.Addy.Trace, PID: 22270 java.lang.RuntimeException: Unable to get provider com.plugin.unityapp.StickerContentProvider: java.lang.RuntimeException: contents.json file has some issues: /data/user/0/com.Addy.Trace/files/stickers.json: open failed: ENOENT (No such file or directory) at android.app.ActivityThread.installProvider(ActivityThread.java:7416) at android.app.ActivityThread.installContentProviders(ActivityThread.java:6956) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6844) at android.app.ActivityThread.access$1300(ActivityThread.java:268) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1982) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:237) at android.app.ActivityThread.main(ActivityThread.java:7814) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1068) Caused by: java.lang.RuntimeException: contents.json file has some issues: /data/user/0/com.Addy.Trace/files/stickers.json: open failed: ENOENT (No such file or directory) at com.plugin.unityapp.StickerContentProvider.readContentFile(StickerContentProvider.java:164) at com.plugin.unityapp.StickerContentProvider.getStickerPackList(StickerContentProvider.java:182) at com.plugin.unityapp.StickerContentProvider.onCreate(StickerContentProvider.java:98) at android.content.ContentProvider.attachInfo(ContentProvider.java:2113) at android.content.ContentProvider.attachInfo(ContentProvider.java:2087) at android.app.ActivityThread.installProvider(ActivityThread.java:7411) at android.app.ActivityThread.installContentProviders(ActivityThread.java:6956)  at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6844)  at android.app.ActivityThread.access$1300(ActivityThread.java:268)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1982)  at android.os.Handler.dispatchMessage(Handler.java:107)  at android.os.Looper.loop(Looper.java:237)  at android.app.ActivityThread.main(ActivityThread.java:7814)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1068)  Caused by: java.io.FileNotFoundException: /data/user/0/com.Addy.Trace/files/stickers.json: open failed: ENOENT (No such file or directory) at libcore.io.IoBridge.open(IoBridge.java:496) at java.io.FileInputStream.<init>(FileInputStream.java:159) at com.plugin.unityapp.StickerContentProvider.readContentFile(StickerContentProvider.java:156) at com.plugin.unityapp.StickerContentProvider.getStickerPackList(StickerContentProvider.java:182)  at com.plugin.unityapp.StickerContentProvider.onCreate(StickerContentProvider.java:98)  at android.content.ContentProvider.attachInfo(ContentProvider.java:2113)  at android.content.ContentProvider.attachInfo(ContentProvider.java:2087)  at android.app.ActivityThread.installProvider(ActivityThread.java:7411)  at android.app.ActivityThread.installContentProviders(ActivityThread.java:6956)  at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6844)  at android.app.ActivityThread.access$1300(ActivityThread.java:268)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1982)  at android.os.Handler.dispatchMessage(Handler.java:107)  at android.os.Looper.loop(Looper.java:237)  at android.app.ActivityThread.main(ActivityThread.java:7814)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1068)  Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory) at libcore.io.Linux.open(Native Method) at libcore.io.ForwardingOs.open(ForwardingOs.java:167) at libcore.io.BlockGuardOs.open(BlockGuardOs.java:252) at libcore.io.ForwardingOs.open(ForwardingOs.java:167) at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7691) at libcore.io.IoBridge.open(IoBridge.java:482) at java.io.FileInputStream.<init>(FileInputStream.java:159)  at com.plugin.unityapp.StickerContentProvider.readContentFile(StickerContentProvider.java:156)  at com.plugin.unityapp.StickerContentProvider.getStickerPackList(StickerContentProvider.java:182)  at com.plugin.unityapp.StickerContentProvider.onCreate(StickerContentProvider.java:98)  at android.content.ContentProvider.attachInfo(ContentProvider.java:2113)  at android.content.ContentProvider.attachInfo(ContentProvider.java:2087)  at android.app.ActivityThread.installProvider(ActivityThread.java:7411)  at android.app.ActivityThread.installContentProviders(ActivityThread.java:6956)  at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6844)  at android.app.ActivityThread.access$1300(ActivityThread.java:268)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1982)  at android.os.Handler.dispatchMessage(Handler.java:107)  at android.os.Looper.loop(Looper.java:237)  at android.app.ActivityThread.main(ActivityThread.java:7814)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1068) 

这是android studio的LOGCAT中的崩溃日志。从脚本中的统一,我使用唤醒函数并将 json 文件写入内部存储,但可能在应用程序启动期间,插件没有使用内部存储检测到 jsonString file_name=context.getFilesDir() + "/"+"stickers.json";

JSON 文件内容

{
  "android_play_store_link": "",
  "ios_app_store_link": "",
  "sticker_packs": [
    {
      "identifier": "1",
      "name": "Adi",
      "publisher": "Jane Doe",
      "tray_image_file": "Trayicon_Cat1.png",
      "image_data_version":"1",
      "avoid_cache":false,
      "publisher_email":"",
      "publisher_website": "",
      "privacy_policy_website": "",
      "license_agreement_website": "",
      "stickers": [
        {
          "image_file": "Formidable.webp",
          "emojis": ["☕",""]
        },
        {
          "image_file": "Awful.webp",
          "emojis": ["",""]
        },
        {
          "image_file": "Athletic.webp",
          "emojis": ["☕",""]
        }

      ]
    }
  ]
}
4

0 回答 0