I'm porting a C++ Linux application to Android using NDK and testing using the emulator. The application tries to create an /etc/myApp directory and fails because the component /etc does not have the necessary write file system permissions required to create.
Should my application be able to create such a directory? I used adb to inspect and try to make the directory from the shell and it fails too:
mkdir failed for myApp, Read-only file system
Does the file system being read only in adb mean it is also read-only for my application?
Can my application expect to create this directory and if not, is there another location that is more appropriate?
No, you should not be able to do that. Android apps are not permitted to write to system directories.
You should probably create this directory within your app's private storage area. Generally it is best to discover the path of that from Java (don't hardcode it) and then pass it through to the native side.
Depending on your needs for the file, the ExternalStorage might also be an option, especially during development where you might need to easily modify it manually - though keep in mind that others things will be able to change it there, too. Again, you should determine the path on your particular device in Java and then pass that through to the native code.
Related
I'm developing an android application using Qt. As far as I understood the default path for deployment and installation of application is /data/user/0/... and this path is inaccessible unless my android device has been root. I would like to know if there is any possible way to change this path and make it accessible since I need to access some of the files in this directory.
Thank you some much for your help in advance!
As long as your device is not rooted, you can not access this location from other applications. If you just want to do it for development purposes, you can simply run
adb root
from the command line to access that location from the development host and thorough the adb.
For example, you will then have access to push/pull files from that location using a command like this in Windows
adb pull /data/user/0/.../somesqlite.db %USERPROFILE%\Desktop\
I fully read the documentation of Cordova plugin filesystem. Furthermoe I've been googling without though conclusive explanations.
Particularly, how do the paths stored in the variables in cordova.file.* map the real folders in Android filesystem that you can see on any File Explorer? For example, I cannot save a file into the Downloads folder. I tried file:///android_asset/Download without success.
What exactly is the protocol file:/// and the path file:///android_asset/? What is the "application's sandbox"?
For example, I save a file into cordova.file.cacheDirectory because I need to deal with a temporary file, but I tried to find such file within a file explorer, and I can't find such file. Is it hidden somehow? I can't find it neither in /data/data/<app-id>/cache nor in "file:///data/user/0/com.form.parking.violation/cache/" (real value of that string).
I know, it's too many questions, but I will plan to make this a canonical question, since clear and pedagogical information is very scarce.
Using a 'File Manager' app on device won't give you access to items listed as 'Private' in the documentation you've listed. 'Private' means no other app should be able to see the contents which is sort-of described by "application sandbox". Normally a sandbox is for describing an environment which something can't get out of. If you aren't familiar with multi-user environments it can also mean that others without the right level of permission can't see in.
And unless the device you are testing on is rooted, you won't be able to see those 'private' files like the SQLite database and other files you are interested in unless you use adb from the Android SDK with the adb shell command run-as as described here:
Android ADB access to application databases without root
Note that to grab the files you need to have your Cordova app debuggable by Android Studio (debug mode).
For what is file:///android_asset/ I'll refer to this SO question:
What does file:///android_asset/www/index.html mean?
After looking all over google I haven't found a way of accessing Android internal storage from python.
I need to store an image generated by kivy app. I would like my python code to be able to navigate to a root user dir, create an app specific dir (if not found) and save the image there.
Simply creating a file with my device's path doesn't work. Should I be setting permissions for internal storage access? I'm lost with it.
You have something wrong in your code, you didn't paste the code nor logs, so... let's get it another way.
You can read/write with python just fine in the app folder (/data/data/org.something) with using:
app_folder = os.path.dirname(os.path.abspath(__file__))
To write to SD card you'll need to use App.user_data_dir, which as you can see in the docs on android gets you /sdcard/<app_name>. Not sure if it automatically creates it if it's missing, so it needs checking out probably. However, if it doesn't exist, just create it with python:
os.mkdir(App.user_data_dir)
I'm trying to create an android application that writes to a text file that can later be accessed once a button is pushed.
The past week i've tried a bunch of methods that people suggest to write to the internal storage, and sometimes it appears to work (using an outputwriter, and also a File class?), but i'm never able to locate the file on the Android device I test-run it on.
I'm rather new to development for Android, so all this is confusing to me.
Thanks
If by "internal storage" you mean what the Android SDK refers to as internal storage, this is not possible. Files that you create there are only accessible to your app, not by file managers on or off the device.
If by "internal storage", you mean what the Android SDK refers to as external storage, you need to:
Get a File pointing to a directory on external storage, such as calling getExternalFilesDir() on some Context, like your Activity
Create that directory if it does not exist
Create a File object pointing to the file you want to create, off of that directory
Use standard Java file I/O to write to the location identified by that File
Use MediaScannerConnection and its scanFile() method to tell Android "hey, I just put a file on external storage, please index it so it shows up in file managers"
Also:
Ideally, you do the disk I/O on a background thread, so you do not freeze the UI while that work is going on.
Depending on your minSdkVersion and where you choose to write the file, you may need the WRITE_EXTERNAL_STORAGE permission
Depending on your targetSdkVersion, you may need to ask for WRITE_EXTERNAL_STORAGE at runtime
You CAN access internal storage sometimes, but this depends on your phone.
You always can get data from internal memory for rooted phones (need root).
Files are in the folder /Android/data/
Some vendors allows you to run root shell on non-rooted phone through adb
(I saw this behaviour on Explay tabet) just run adb shell su to
test. Then you can copy your file from internal storage to public
with shell commands.
adb pull may also work in this case. (Again
vendor dependent)
hello guys i need small help in understanding file system of android
Now in windows for example we create files using paths like "c:/mytextfile.txt" or "c:/folder/mytextfile.txt".Now how can i access files and folders in android i mean whats the path like.
Does the phone support file browser instead of relying on third party apps??
Android does not have a native file browser, but there are numerous third-party ones (Astro comes to mind). The filesystem of Android is that of Linux; the path separator is / and the FS grows from a single root called /. So, you have your app packages under /data/apps, and so forth. Unless the phone is jailbroken ("rooted"), you won't get to see the whole filesystem - permissions get in the way. This applies to all Android applications, they are sandboxed - that is, they don't get access the whole filesystem. There are API calls to get the path to the current application's sandbox directory.