Is there any other way to write a file somewhere else than on the SD card?
I tried many different path on the filesystem but fopen always return NULL, except for any file that I write/read inside the /sdcard/...
Is there something else equivalent to:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
That allow you to write like on the file system or something?
Android External Storage Example CodegetExternalFilesDir(): It returns the path to files folder inside Android/data/data/application_package/ on the SD card. It is used to store any required files for your app (like images downloaded from web or cache files).
You need to provide the WRITE_EXTERNAL_STORAGE permission.
The Native Development Kit (NDK) is a set of tools that allows you to use C and C++ code with Android, and provides platform libraries you can use to manage native activities and access physical device components, such as sensors and touch input.
You can always access the directory in which your app is placed.
This directory is usually :
data/data/<Your_package_name_usually
com.android.appName>/files/<your_filename>
but better use getFilesDir()
to
ensure valid filepath with future
version changes of android.
If you wanna use external storage instead, make sure you place this line of code in your app manifest to gain permission.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Then use the sdcard directory :
getExternalStorageDirectory();
//usually : /sdcard/<your_filename>
Otherwise, you can root the device
and gain access to the whole
filesystem. But you'll have to
request all of your app users to get
root access on their device before
using your application. Some games
use this way of doing things, but I
wouldn't recommend it. If you still
wanna do this, lookup Superuser
app for android, it's free and
trustable.
Once you have the android directory, just create a JNI native method that receive a jstring parameter, and you set it up inside your native code. Convert it into a std::string, and you'll be ready to use it with fopen().
//Inside your java activity
File f = this.getApplicationContext().getFilesDir();
LibLoader.setupArchiveDir(f.toString());
//Inside your JNI wrapper
JNIEXPORT bool JNICALL Java_com_android_appName_LibLoader_setupArchiveDir(JNIEnv * env, jobject obj, jstring dir)
{
const char* temp = env->GetStringUTFChars(dir, NULL);
std::string stringDir(temp);
// Will be receive as a std::string inside C++ code
MyNativeObjectInC++->SetupArchiveDir(stringDir);
}
Edit :
You might need to manualy creates the /files in C before being able to use it on the first time :
int result_code = mkdir("/data/data/com.app/files/", 0770)
Cheers !
You have access to two locations from within your app, the SDCard and a special data directory for your app. Any other place is off limits unless you have root access.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With