Allan
Allan

Reputation: 23

Config file on Android

New to Android, working on an app for Vuzix M300s. The app needs to access a file that contains the IP address and port of a web server. I believe I will need to manually place a pre-configured file on the M300s using adb shell, but I cannot figure out where on the device to place it so that the app can find it. Via Android Studio 3.1.3, I have placed a file in the assets folder which I can open & read, but using adb shell I cannot locate it. (I get permission denied for a lot of actions like ls). How do I get a file on there? Or is there a better way?

Upvotes: 1

Views: 3296

Answers (2)

Allan
Allan

Reputation: 23

Figured it out.
To move/copy a file to the M300s for an application

  1. move the file to the device (in the sdcard folder)
    • .\adb push C:\temp\file.cfg /sdcard/
  2. move the file from /sdcard/ to the desired location
    • a) go into the shell
      • '> .\adb shell
    • b) change to the application's permissions
      • $ run-as com.foobar.appname
    • c) copy the file into the app's 'files' folder
      • $ cp /sdcard/file.cfg files/

Within my app, I was able to read this with

FileInputStream fin = openFileInput("file.cfg");
InputStreamReader rdr = new InputStreamReader(fin);
char[] inputBuffer = new char[100];
int charsRead = rdr.read(inputBuffer);
String fileContents = new String(inputBuffer);
rdr.close();
Log.i(method, "charsRead: " + charsRead);
Log.i(method, "fileContents: " + fileContents);

Upvotes: 0

Code-Apprentice
Code-Apprentice

Reputation: 83527

Note that the assets folder in your project only exists on your development machine. The contents of this folder are packaged into the APK file when you build your app. In order to read any of these files, you need to use Context.getAssets() as explained in read file from assets.

Upvotes: 2

Related Questions