LiquidCore icon indicating copy to clipboard operation
LiquidCore copied to clipboard

LiquidCore file system and Android/iOS app

Open boby1975 opened this issue 4 years ago • 1 comments

How to get direct access to the LiquidCore virtual file system from Android/iOS apps? I mean read/write files to the folder /home/public/data of LiqiudCore directly from the application without a microservice. Is this somehow possible? Thank you in advance.

boby1975 avatar Apr 21 '20 11:04 boby1975

Hi @boby1975 ,

The easiest way to do it is to use a method that I just added about two weeks ago -- #156

Android:

  android.content.Context context = ...;
  MicroService service = ...;

  // Expose the Android app's files directory to LiquidCore
  final String dir = context.getFilesDir().getAbsolutePath();
  service.getProcess().exposeHostDirectory(dir, Process.kMediaAccessPermissionsRW);

  // Write a file
  final String filename = "testExposeHostFS.txt";
  final String fileContents = "These are the file contents";
  try (FileOutputStream fos = context.openFileOutput(filename, Context.MODE_PRIVATE)) {
    fos.write(fileContents.getBytes());
  }

  // Tell MicroService about it
  service.emitString("filesDirPath", dir);

JavaScript:

  const fs = require('fs')

  LiquidCore.on('filesDirPath', (path) => {
     // Read the file
     let contents = String(fs.readFileSync(path + '/testExposeHostFS.txt'))
     console.log(contents)
  }

ericwlange avatar Apr 21 '20 12:04 ericwlange