Is there a recommended path to save files on SD Card? - android

I see files from apps all over in the sd card so there doesn't seem to be an android standard. But when saving app related files externally, is there a recommended directory path to use or a common practice?
ie. (sdcard)/myapp/ or (sdcard)/apps/myapp/

Simply use Environment#getExternalStoragePublicDirectory(String type) with the various String types for a specific folder.
This creates folders in .../sdcard/Android/data/my.package.name/<type>.
From the Developer's Guides:
In API Level 8 or greater, use getExternalStoragePublicDirectory(), passing it the type of public directory you want, such as DIRECTORY_MUSIC, DIRECTORY_PICTURES, DIRECTORY_RINGTONES, or others. This method will create the appropriate directory if necessary.
If you're using API Level 7 or lower, use getExternalStorageDirectory() ...

You should first off use:
Environment.getExternalStorageDirectory();
To figure out the sdcard location. From there typically it is just your appname as the folder to store any files in. Or so that appears to be the standard looking at my phone.

I would do like this:
Environment.getExternalStorageDirectory() + "Android/data/" + yourpackagename;
if you check this file, you can see lots of apps here.

Related

Recommended locations to store app licence file and database?

Where would the recommended location be to store an app licence file and a SQLite database for Android? Also, what are the constants used to point to those locations?
Note that the location(s) must be accessible without having to root the device, so the app data folder is not an option. I need to be able to access the files via a PC using a standard file manager.
I noticed that on the root folder of the device, there is a folder called "db" where other apps seem to store data. Is that a good location to store my db? If so, what is the Environment constant that points to it?
Depending on the level of security you want to achieve, there are numerous approaches to this issue.
At it's simplest you could store them at any folder on you external storage(Documents,Downloads e.t.c) or on your SD card provided that the user has given you permission AND has himself selected the path since newer Android Versions have reworked(restricted) the way an app can read/write from/to an SD card.
If for example you want to store it to "documents" folder you could do the following:
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS),".dir");
This selects the "Documents" folder and creates a folder called dir. Note the "." before "dir" , meaning that the folder will be invisible as a first level of security. From here on you can create any number of directories or files you desire.

DownloadManager directly into external storage directory

I'm trying to download a big zip file directly inside the external storage directory, for example
"/mnt/sdcard/Android/data/com.vexdev.audioguida.app/files/data"
i guess i should be using
DownloadManager.Request.setDestinationInExternalPublicDir(path, fileName)
but i don't know how to provide it with a path that is consistent across different android devices.
AND i'm also trying to get a path to access this file later, like this:
File file = new File(path + fileName);
i'm asking here because i know that those two methods are expecting different rooted paths, because i tried to provide a path like this:
Application.getAppContext().getExternalFilesDir(Environment.getDataDirectory().getAbsolutePath()).getAbsolutePath();
but the DownloadManager was not putting the files where the File constructor was searching it later. (It was downloading in the wrong directory actually!)
I'm looking for a way to download those files directly in the right directory, without having to move them.
i guess i should be using DownloadManager.Request.setDestinationInExternalPublicDir(path, fileName)
Not for the location you suggested. The closer match would be setDestinationInExternalFilesDir().
but i don't know how to provide it with a path that is consistent across different android devices.
There isn't even a path that will be consistent within one device, as different accounts will use different paths. For example, the path you typed into your question might be used on some devices for some accounts, but the details will vary.
i'm also trying to get a path to access this file later
That is covered in the DownloadManager.Request documentation:
setDestinationInExternalFilesDir() maps to getExternalFilesDir() on Context
setDestinationInExternalPublicDir() maps to getExternalStoragePublicDirectory() on Environment
i tried to provide a path like this
That is not how you use getExternalFilesDir(). Please read the JavaDocs to see what valid values are for the parameter to that method. getDataDirectory() is not a valid value.

How do we get the file separators used by the different file systems?

Good afternoon all,
From what I understand, Android has (at least) 2 file systems. One is for "internal" storage (e.g. /data and /system) and the other for "external" storage (e.g. /mnt/sdcard),
This implies that when we save files to "internal" storage (Context.getFilesDir, Context.getCacheDir), the file separator used may be different from when we save files to "external" storage (Context.getExternalFilesDir, Context.getExternalCacheDir, Environment.getExternalStoragePublicDirectory), yet java.io.File.separator only seems to give us information on the "default" filesystem used.
How do we get the different file separators used by the different file systems on Android?
I've tried java.nio.file.spi.FileSystemProvider.installedProviders() but it looks like android doesn't have this package.
Is there anyway to get a list of filesystems on Android?
From what I understand, Android has (at least) 2 file systems. One is for "internal" storage (e.g. /data and /system) and the other for "external" storage (e.g. /mnt/sdcard),
That was accurate for Android 1.x and 2.x. As of Android 3.0, external storage is merely a directory inside of internal storage.
This implies that when we save files to "internal" storage (Context.getFilesDir, Context.getCacheDir), the file separator used may be different from when we save files to "external" storage
Of course not. Separators are a feature of an operating system, not a file system.
Is there anyway to get a list of filesystems on Android?
Nothing that is supported. SDK applications should only use internal and external storage, such as via the APIs you cited in your answer.
Hmm, I've heard of mountaing the external drive on a NTFS which is why I think that they can be different
You are mistaken. File and path separators have nothing to do with the format of the file system.
However its not just the file separators, the file delimiters (path separators) are also different between different file systems.
You are mistaken. File and path separators have nothing to do with the format of the file system.
If you're concerned about putting a hardcoded file separator in your code you can use File.separator in place of hardcoded "/". This will give you the file separator used by Android without having concern of whatever file system lies underneath

Best/usual practices for android app directories

Is there any sort of consensus on creating/using directories for storing/accessing data for android apps.
For example on windows a new application (say MyApp) would go in the "Program files" directory in a new "MyApp" directory.
I'm writing an app that allows the user to analyse photo and xml files. Is it usual to expect MyApp to just look for those files in Environment.getExternalStorageDirectory(), require the user to move the photos/xmls to Environment.getExternalStorageDirectory()/MyApp or something else? Should you always just provide a file explorer to look anywhere on the device?
I can do any of the above means of accessing but it's better to stick with the user's expectations.
Any pointer to a UI preferred practices would be useful (assuming they're widely followed).
To store your pictures (assuming you are using API >= 8), you should use:
getExternalFilesDir(Environment.DIRECTORY_PICTURES);
Check the documentation which contains an example.
If you want to keep your images in a separate folder you can create one named after your package name inside this one.
Otherwise, if you are using API <= 7, use getExternalFilesDir() to obtain the root of the tree from where you should start looking for images.

Save mp3 file as ringtone

I have made an app that you puss a button and hear a ringtone!How can i add an option to save it to your device as a ringtone,if you like?thanks
http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
It should be stored on a sdcard /Ringtones
If you want to save files that are not specific to your application and that should not be deleted when your application is uninstalled, save them to one of the public directories on the external storage. These directories lay at the root of the external storage, such as Music/, Pictures/, Ringtones/, and others.
and
In API Level 8 or greater, use getExternalStoragePublicDirectory(), passing it the type of public directory you want, such as DIRECTORY_MUSIC, DIRECTORY_PICTURES, DIRECTORY_RINGTONES, or others. This method will create the appropriate directory if necessary.
I read somewhere that you can put them in /media/ringtones but this isn't documented anywhere else. So you should use what they wrote in the docs.

Categories

Resources