Is there a way to get the application's directory to save private data to a file for my application directly from the NDK? In other words, I need an equivalent of the Java function mContext.getFilesDir().
I have noted that other posts such as this one:
Android NDK Write File
mention what is the 'usual' place for this directory to be. But for it to be correct in all versions and all devices, an equivalent system call is surely necessary.
I am currently using a NativeActivity and no Java at all.
Thank you for your time.
I have custom helper class for accessing various paths, for the data directory I do this:
Read /proc/self/cmdline or /proc/**MyPID**/cmdline
Append the results from #1 to /data/data/
Example results:
/data/data/com.yourcompany.yourproduct/
If you are using NativeActivity then you have access from native code to an ANativeActivity instance (see <ndk_root>/platforms/android-9/arch-arm/usr/include/android/native_activity.h) which has internalDataPath and externalDataPath members.
Related
I am currently creating an android library that uses json commands for communication with another library. I would like to extract these commands from code and save them in separate files for better readability.
Where do I save those files when there is no asset directory and how do I read them?
Edit: I have found an answer to my question:
how to access resources in a android library project
Have a look at Internal Storage. The files saved here are only available to your app. It uses the Java File API to read an write using the FileInputStream and FileOutputStream
UPDATE:
As per the discussion in the comments, OP was looking for a method to ship a JSON file with the library. In light of that:
I'm not sure if library modules support raw resources. If they do, you might want to use that but it will increase the size significantly. You could also fetch the file from a server the first time you're the library is used, keeping track of that using a SharedPreference entry.
How can I get reach to the native methods called from Java side in Android? My problem is specifically related to AudioRecord class in Android Media package. I read the source code of AudioRecord.java. I found out that most of the jobs is performed by native methods, such as:
native_setup(...), native_start(...), native_stop(...), native_read_in_byte_array(...), native_read_in_direct_buffer(...)
I downloaded Android source code but I could not reach these methods. And I don't actually know the way to reach them. I seek for these methods in libraries I found in source code directories, but I couldn't success. If anybody may have an idea, I would be appreciative to hear. Thanks...
I think I found them. After using the Linux command
grep -r "native_read_in_direct_buffer" ./ANDROID_SOURCE/.*
I found the corresponding cpp files.
AudioRecord.cpp is located in: ~/ANDROID_SOURCE/frameworks/av/media/libmedia/ directory,
android_media_AudioRecord.cpp is located in ~/ANDROID_SOURCE/frameworks/base/core/jni directory.
I wanted to share it as a reference to other possible programmers willing to reach the same/similar source files.
Android can use .so file through JNI. The native function in C/C++ is bound to a Java function.
I want to use other developers' .so file (I don't have the source code). I know the interface of the native function, but I have faced some difficulties. It seems that the .so file is hard coded to bind for a certain package name.
Is there any way for me to use other developers' .so file?
If not, is it possible to decompile the .so file and make it work?
You could create a package with the name that the shared object expects, in which you have a wrapper class whose only purpose is to load the library and provide the native methods to clients using this class.
Import that package into your own activity's package (or wherever you want to use it) and call the native methods through the wrapper class.
I have an external compiled static C++ library that I'm using in my android application. This library is reading a file. I want to know if there is a way I can "redirect" the function that's reading the file so that it reads another file.
So if it does:
fopen("myfile.txt", "rb");
I want to intercept it and to do this instead:
fopen("myotherfile.txt", "rb");
In Objective-C I use MethodSwizzling. Is there something similar I can do in C++ or the android NDK?
Short of editing the binary (with uncertain results), your best option is to use a symlink... if you're just doing it for development purposes, you could use adb shell into your test device to create the symlink.
http://developer.android.com/guide/developing/tools/adb.html#issuingcommands
How about contact the author of the library and ask them to introduce a parameter? Having hard-coded file paths is a lousy design anyway, the library will be better off.
Through the housekeeping stuff my android application has to do is to read and write some files and sometimes to delete others and create new ones. The files in cause have all "sdcard/" as root. Is it safe if I perform all this task using Java style functions ?
To me it is very handy to do it this way but I've read that not always the java functions can be applied when it comes to files in Android.
Yes you can access /sdcard with regular java.io classes in Android. But don't hardcode this root; use Environment.getExternalStorageDirectory() as the root.