Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's perfectly possible, but not without some Java code.</p> <p>EDIT : the following is offered as an alternative to a database. Being able to read and write persistent data to/from files from native code would be a lot more flexible than a database...</p> <p>Assuming you want to store and retrieve some data from a file (binary or plain text) residing on the filesystem these would be the steps to take:</p> <ol> <li><p>JAVA : get the storage location for your app and check if it's available for reading and writing </p></li> <li><p>JAVA : if the above is positive, pass it to the native layer through JNI</p></li> <li><p>NATIVE : use the storage params to read/write your file</p></li> </ol> <p>Ok, so far the abstract; lets get to the code:</p> <p>1A) retreiving and checking storage:</p> <pre><code>private boolean checkExternalStorageState(){ String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { // We can read and write the media android.util.Log.i("YourActivity", "External storage is available for read/write...", null); return true; } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { // We can only read the media : NOT ok android.util.Log.e("YourActivity", "External storage is read only...", null); return false; } else { // Something else is wrong. It may be one of many other states, but all we need // to know is we can neither read nor write android.util.Log.e("YourActivity", "External storage is not mounted or read only...", null); return false; } } </code></pre> <p>Get the storage location :</p> <pre><code>private get storageLocation(){ File externalAppDir = getExternalFilesDir(null); String storagePath = externalAppDir.getAbsolutePath() } </code></pre> <p>1B) you also might want to check if a file exists (you can also do this in the native part)</p> <pre><code>private boolean fileExists(String file) { String filePath = storagePath + "/" + file; // see if our file exists File dataFile = new File(filePath); if(dataFile.exists() &amp;&amp; dataFile.isFile()) { // file exists return true; } else { // file does not exist return false; } } </code></pre> <p>2) Pass it to the native layer:</p> <p>JAVA part:</p> <pre><code>// Wrapper for native library public class YourNativeLib { static { // load required libs here System.loadLibrary("yournativelib"); } public static native long initGlobalStorage(String storagePath); ...enter more functions here } </code></pre> <p>NATIVE part:</p> <pre><code>JNIEXPORT jlong JNICALL Java_com_whatever_YourNativeLib_initGlobalStorage(JNIEnv *env, jobject obj, jstring storagePath) { jlong data = 0; // convert strings const char *myStoragePath = env-&gt;GetStringUTFChars(storagepath, 0); // and now you can use "myStoragePath" to read/write files in c/c++ //release strings env-&gt;ReleaseStringUTFChars(storagePath, myStoragePath); return data; } </code></pre> <p>How to read/write binary or text files in c/c++ is well documented, I'll leave that up to you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload