在 Android 中为微流创建示例应用程序时出现问题

Issue in creating a sample app for microstream in Android

我正在尝试为微流构建一个示例 android 应用程序,但出现错误。 Gradle 依赖关系

dependencies {
    implementation 'one.microstream:microstream-storage-embedded:05.00.02-MS-GA'
    ...
}

错误

    Process: com.example.microstream, PID: 6369
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.microstream/com.example.microstream.MainActivity}: one.microstream.persistence.exceptions.PersistenceException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        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:858)
     Caused by: one.microstream.persistence.exceptions.PersistenceException
        at one.microstream.persistence.internal.PersistenceTypeDictionaryFileHandler.writeTypeDictionary(PersistenceTypeDictionaryFileHandler.java:107)
        at one.microstream.persistence.internal.PersistenceTypeDictionaryFileHandler.writeTypeDictionary(PersistenceTypeDictionaryFileHandler.java:203)
        at one.microstream.persistence.internal.PersistenceTypeDictionaryFileHandler.storeTypeDictionary(PersistenceTypeDictionaryFileHandler.java:209)
        at one.microstream.persistence.types.PersistenceTypeDictionaryExporter$Default.exportTypeDictionary(PersistenceTypeDictionaryExporter.java:93)
        at one.microstream.persistence.types.PersistenceTypeDictionaryManager$Exporting.exportTypeDictionary(PersistenceTypeDictionaryManager.java:267)
        at one.microstream.persistence.types.PersistenceTypeDictionaryManager$Exporting.synchUpdateExport(PersistenceTypeDictionaryManager.java:258)
        at one.microstream.persistence.types.PersistenceTypeDictionaryManager$Exporting.registerRuntimeTypeDefinitions(PersistenceTypeDictionaryManager.java:324)
        at one.microstream.persistence.types.PersistenceTypeHandlerManager$Default.synchUnvalidatedRegisterTypeHandlers(PersistenceTypeHandlerManager.java:662)
        at one.microstream.persistence.types.PersistenceTypeHandlerManager$Default.registerTypeHandlers(PersistenceTypeHandlerManager.java:644)
        at one.microstream.persistence.types.PersistenceTypeHandlerManager$Default.initialRegisterTypeHandlers(PersistenceTypeHandlerManager.java:687)
        at one.microstream.persistence.types.PersistenceTypeHandlerManager$Default.synchInternalInitialize(PersistenceTypeHandlerManager.java:968)
        at one.microstream.persistence.types.PersistenceTypeHandlerManager$Default.initialize(PersistenceTypeHandlerManager.java:941)
        at one.microstream.storage.embedded.types.EmbeddedStorageFoundation$Default.createEmbeddedStorageManager(EmbeddedStorageFoundation.java:791)
        at one.microstream.storage.embedded.types.EmbeddedStorage.createAndStartStorageManager(EmbeddedStorage.java:649)
        at one.microstream.storage.embedded.types.EmbeddedStorage.start(EmbeddedStorage.java:467)
        at one.microstream.storage.embedded.types.EmbeddedStorage.start(EmbeddedStorage.java:317)
        at com.example.microstream.MainActivity.microstreamStorageManager(MainActivity.kt:52)
        at com.example.microstream.MainActivity.checkPermission(MainActivity.kt:78)
        at com.example.microstream.MainActivity.onCreate(MainActivity.kt:47)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        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:858) 
     Caused by: one.microstream.exceptions.IORuntimeException: java.nio.file.AccessDeniedException: storage/PersistenceTypeDictionary.ptd
2021-10-20 07:26:34.758 6369-6369/com.example.microstream E/AndroidRuntime:     at one.microstream.afs.nio.types.NioIoHandler$Default.specificCreate(NioIoHandler.java:324)
        at one.microstream.afs.nio.types.NioIoHandler$Default.specificCreate(NioIoHandler.java:1)
        at one.microstream.afs.types.AIoHandler$Abstract.create(AIoHandler.java:701)
        at one.microstream.afs.types.AWritableFile.create(AWritableFile.java:102)
        at one.microstream.persistence.internal.PersistenceTypeDictionaryFileHandler.writeTypeDictionary(PersistenceTypeDictionaryFileHandler.java:91)
            ... 33 more
     Caused by: java.nio.file.AccessDeniedException: storage/PersistenceTypeDictionary.ptd
        at sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:214)
        at java.nio.file.Files.newByteChannel(Files.java:361)
        at java.nio.file.Files.createFile(Files.java:632)
        at one.microstream.afs.nio.types.NioIoHandler$Default.specificCreate(NioIoHandler.java:320)
            ... 37 more

如何解决这个问题?

日志中描述了该问题。您正在尝试写入应用程序无法存储数据的位置。

获取写入数据的路径,可以使用例如:

Context instrumentationContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
targetDirectory = instrumentationContext.getFilesDir().toPath();
targetDirectory = targetDirectory.resolve("storage");
targetDirectory.toFile().mkdir();

之后:

EmbeddedStorageManager storage = EmbeddedStorage.start(root, targetDirectory);