In an Android app, using NDK, I'm accessing a local file using fopen, fclose, etc. My app started out as HellloJNI (the supplied example). Accessing the file actually worked initially. The file path I used was "/data/data/com.example.hellojni/files/Test.txt".
Naturally I don't want my app to be called HelloJNI forever, so I changed the package name (manifest/#package in AndroidManifest.xml) from com.example.hellojni to com.example.mytest. Accordingly, I use a different file path: "/data/data/com.example.mytest/files/Test.txt". But the same call to fopen("[...]", "w+") that used to succeed now fails.
Any ideas?
It works after manually creating the files folder below /data/data/com.example.mytest. In a way, it's obvious, but I still don't fully understand it, because I never created explicitly the files folder below /data/data/com.example.hellojni. So where did that one come from?
Edit:
Apparently calling getFilesDir creates the Files folder implicitly, so that mystery is solved :-)
Related
So, I'm pretty new to chaquopy and was wondering if there's anyway to get newly generated (ie: written post-build, while the app is running) .py files to run?
What I mean is:
Say I save the contents of a textEdit view into a file named 'newFile.py', and it goes into some specified external storage location. Is there anyway to specify that externalStorageDir in advance via source sets (which, as far as I can tell, only apply to src directories and not externalStorage) or something else so that getModule() can later locate it, making the functions in the newly written file callable?
Pretty niche question I know, but this package has me thinking of all kinds of possibilities.
You can modify sys.path at runtime, like this:
py.getModule("sys").get("path").callAttr("append", yourExternalDirectory);
And now any Python files in that directory can be loaded using py.getModule.
I'm trying to use some executables inside my app. This executable is C++ based (with images and logs as output), there may be other ways to use C++ code, but in this specific context, it might be the best to use it as an executable which only needs arguments. (NDK f.e. will be too much changing of the used library and I'm hoping there may be a way to parallelize this process some days)
My research showed that some other people were looking for similar problems. It seems that the best way was to add these external programs inside the assets folder (app/src/main/assets). That is what I did, but now I'm not quite sure how to get my data inside the assets folder. Some posting said that these files have to be extracted in another folder to get them work.
So I tried this method:
How to copy files from 'assets' folder to sdcard? (result: app shutdown)
I would like to add some code, but there isn't much for now. As I already told my executable should be inside assets and its name is f.e. exec, I also named it libexec.so, because I read somewhere that you have to name these files like lib"...".so to get them extracted. In general, my aim is to use this executable like:
data/exec
Now to my questions:
How can I get access my assets files?
Or should I use another folder instead of assets, will it be easier to use? How could I add an external executable there?
After some time I managed to get it work through:
https://github.com/flipagram/android-assetcopier
I just had to change something:
Example was:
File destDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
I've changed it to: File destDir = (MainActivity.this).getFilesDir(); - so there is no problem with permissions (I guess).
But be aware: Copying is successful, but I did not manage to run the program successfully. Maybe the whole process will get killed through a security manager or something else won't work.
I've been driving myself crazy trying to find the answer to a seemingly easy question.
I am trying to create my first app using Cordova. I want to bundle a text file with my app that can be read when the app starts up.
Where do I put this file?
dataDirectory seems like a good place, but where is it? Documentation says /data/data/<app-id>/files but where are those data directories? Do I create them?
Update 1: Ok, I think I've figured out that dataDirectory should be pretty much just where the path says it should be, at the root of the file system. But the directory doesn't appear to be created automatically and I don't know how to package a file - a JSON file, for instance - and place it in that directory.
Essentially you do not need to create these directories. Those are created once your app is installed by the Android itself.
A typical way to get this done would be that you could first off add those files in your app's assets directory and then at runtime you can copy those file wherever you want.
There is a repository on Githup which provide a similar functionality.
I was looking for a way to add some such folder in my Android Project that do no get compiled up to my apk file.
Why I need it :-
We need to maintain proper documentation for project (that actually
everybody needs to ;) nothing new),
but I find it very irritating to look out for that documentation
folder again and again.
.
I am open for any way i can make dcocumentain folder easily one
click accessible (I am already doing it thru taskbar sortcut. ) But
I want it to get the ease of version control thru Eclipse likewise we
do it for our project
IDE :- Eclipse
I got a way myself :-
I had an idea that if I create a folder in my application project with
some anonymous name that actually android has not listed in its
directories ("I am talking about the default one like "res", "src",
"anim"......)
Then either it should "raise an error" or should "ignore" it while compiling to form apk file
luckily it ignores any such folder. now i can put all the documentation in my project.
NOTE:-
My answer empirically driven
I had tested it by creating a "Docs" folder and then copied 1 GB of
random data (includes almost all type of files we came across from
multimedia to zipped ones).
Then I build the apk and the size of the apk was in-effective of all
this..
but when i copied the same data to assets it was showing a huge change
in size of my apk file..
Consider using Javadoc in addition or, if possible, instead of any other documentation.
So, currently there is no way on Android, but to manually preload dependent shared libs. That's fine with me, not a big deal.
There is also some problem with android buildsystem (prebuild libraries feature), and for that reason I had to name differently different builds of my dependent shared library: libsal.so and libsal-slim.so. Depending on build settings one of these two makes it into the final apk. Then, on java side, I try to load libsal.so and if it fails, then I try to load libsal-slim.so.
It worked all fine, until we stumbled upon a phone that ships that libsal.so in default firmware installed into /system/lib. For that reason, my app stopped working on that phone if it's built with libsal-slim.so, since it tries to load libsal.so first and it loads wrong library from /system/lib.
Question: how can I ensure that only library from my install folder gets loaded? Maybe I can somehow retrieve install folder of my app and "calculate" full path of my shared lib? How can I do that java voodoo magic? I'm big time noob in java and android in general, if this questions looks too naive :) and yes, I don't speak English (or how they say), please rely in c++ [end-of-joke]
libsal.so isn't actual name, I used it only for example.
Using System.load(fullpath_of_the_lib)can probably solve your problem.
But your problem is to get the fullpath_of_the_lib. In fact the android API don't allow you to get the path of a resource. You can only get a stream to read it.
To workaround this limitation, what you can do is the following:
put your lib in your app assets folder.
open a InputStream on your lib : getAssets().open("libsal.so")
create a file named "libsal.so" (or whatever) in your sdcard
copy bytes from the inputstream to the file just created
get the absolute filepath of your "libsal.so" on the sdcard
call System.load(fullpath_of_the_lib_on_the_sd_card)
Of course you only need the last step in the lib is already on the sdcard.
An simpler alternative is maybe renaming your lib to something like libMyVeryUncommonLibraryName.so so that it won't conflict with another library name.