Android directory system - android

I am a little unsure on how to store and load files on Android.
I know that Assets are stored in file:///android_assets/, but this directory is exclusive to every application and read only, right?
Where can/ should I store files that are created on runtime and are exclusive to their app/ should be shared between apps?

In your Manifest file use -
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Then to access external storage use one of methods from the class Environment -
getExternalStorageDirectory()
getExternalStoragePublicDirectory(String type)

Check out the documentation of the Context class. It has several methods that return directories where to put files depending on their nature (private, media, cache, ...).

Related

Where to create a publicly-accessible log file on Android

Our application has a large amount of C++ code that creates its own log file as a simple .txt file. It's a circular buffer so it's limited to the size we specify. It's also placed in whatever directory we specify.
The problem is where to place the file so it can be accessed with ADB or a similar tool (without rooting). If we didn't care about the publicly-accessible part, it seems this would be the logical place to locate the file:
packageManager.getApplicationInfo(applicationContext.getPackageName().dataDir
But since we want to be able to pull the file from a customer's phone for post-mortem debugging, I've tried placing it here:
"/mnt/sdcard/Android/data"
This is problematic for several reasons, but I'm not sure if they're all true. (1) It's hard-coded; (2) Not all Android devices have external storage, although I thought they still mapped it to internal storage? (3) The location isn't app-specific so it won't get uninstalled along with the app. And (4) Runtime permission for EXTERNAL_STORAGE is required.
I believe 1-3 can be solved with something like:
android.content.Context.getExternalFilesDir()
Or is there a better choice?
But I don't believe this will get around #4, which is unfortunate as I'd prefer not to "scare" users with more permission requests.
What's the best way to handle this?
Make sure that you have the permissions to read and write the External SD using this code in the Manifest File:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
And then this string will give you the wanted path:
String directory = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/" + getContext().getPackageName();
/storage/emulated/0/Android/data/com.exemple.yourapp/

accessing files in another apps directory

I am hoping to develop an application that needs to read a log file from another popular application. The log file is in /android/data/com.xxx.xxx/files.
Initially I thought my application would need root, but using two different file managers on an unrooted phone, I can access the /Android/data/com.xxx.xxx/Files/ directory and read/write the files there.
Everything I read online tells me it shouldn't work that way though. Can someone help clarify things?
The only way to do this is with the FileProvider. Here a good example on how to implement this:
https://developer.android.com/reference/android/support/v4/content/FileProvider.html
Remember to add these two lines in the manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
to have the permission to read the files from other dirs.
To read this path:
"/your_sd_path/android/data/com.xxx.xxx/"
As you tested, root acess is not needed.
But for reading app dir in below path:
"/data/app/com.xxx.xxx/"
You should have root acess.
Hope it helps

Place to unzip files in Android

Because of apk maximum size 50MB, i decided to make apk expansion file. I know that I can get any file (inputstream) from this package but I am working in LibGDX, and this doesn't have option to load textures/music from inputstream.
So, I must extract files, load texture/music and then I can delete file.
I think that i need max 50MB space and i have two options:
UNZIP TO:
ContextWrapper.getFilesDir() - it returns internal file, but I can't know how many files can I unzip there, because this storage is shared with all apps.
getExternalStorageDirectory() - it returns external file, but according to website (developer.android.com) "This directory may not currently be accessible"
Which directory will be best to unpack and always avaliable?
If, as you tell us, here there are big quantities of data, so in order to improve user experience (not all phones have huge memory and using internal storage may cause your app is uninstalled) you should use external storage.
You have to care about couple of things:
Add permissions to read / write
Check file availability starting app
Adding permissions:
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
...
</manifest>
Checking file availability
And starting the app, be careful and check if external storage is available, and show message otherwise in order to avoid exceptions:
Caution: External storage can become unavailable if the user mounts the external storage on a computer or removes the media, and there's no security enforced upon files you save to the external storage. All applications can read and write files placed on the external storage and the user can remove them.
Use Context.getCacheDir() to get access to a private directory where you can extract files. This should be considered as a cache directory, so you should check for the file on startup and extract it again if it is missing.
This method doesn't require any additional permissions and is more secure. Also, the directory is guaranteed to exist by the Android APIs.

Can my android application modify files and documents stored on the device?

Is it possible to write an android application than can gain access to files and documents on an android device and modify them (delete if possible), not necessarily resource files used by the OS but general user documents, provided that the user of the device allowed the application to do this?
PS. This is not for any unethical use but for academic purposes. :)
As long as you have the permission for it, you'll have no issues.
The OS segregates storage into two categories - Internal and External. Quoting the documentation:
External storage is the best place for files that don't require access
restrictions and for files that you want to share with other apps or
allow the user to access with a computer.
In order to read and write files there, you'll need this permissions on your manifest file:
<manifest [...]>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>
WRITE_EXTERNAL_STORAGE is API Level 4, but 'starting API level 19, this permission is not required to read/write files in your application-specific directories returned by getExternalFilesDir(String) and getExternalCacheDir()' - catch being that files that your application creates there are considered private to the application.

Android: Can write to lib dir?

Is it possible to programmatically copy a file into, or create a symbolic link in the /data/data/application_package/lib directory?
When trying to write to the lib directory, I always get permission denied error.
I have defined
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
You cannot write to lib/, however you do not need to.
You can write to any non-reserved location within your your application's private storage area, and you can load a native library from any file on the device for which you have read and execute permission, using System.load() with the full pathname, rather than System.loadLibrary() with the short library name.
The external storage (sdcard) is mounted with a non-executable flag, but a file in your private storage directory would be a workable solution. Just be sure to make it only writable by your application, so that something else can't change it behind your back (it is because you cannot protect external storage files from such modification that the external storage is non-executable)
if you want load so file ,you can use system.load(); you can copy so to data/package name/libs
and use system.load() to load so

Categories

Resources