Android save app settings/data in Internal/External Storage - android

I need a little help with understanding what can I do and cannot in android. I'm working on application which needs to ask user in first start to select a device (internal/external storage) where to save the data which my application is downloading over internet. So I'm trying to find answer for a few questions about this issue :
Is there any limit for data in internal/external storage in Android for a single application.
Can I set the directory of sqlite database which my app is using and If I can, is it a good practice or not.
What should I consider when user decide to change the destination of application's data from internal to external storage. Should I create all the folders and etc. again or Android platform is doing it automatically?
Thanks in advance!

Your limit on external storage(SDCARD) is determined only by its capacity, I'm not sure about the internal storage, maybe it's the same situation.
You can extend SQLiteOpenHelper and create your own DB adapter, which will manage your database and store it on SD card or /data/databases directory. I think that it's better not to place database on SDCARD, because any other app can access it there. On the other hand, /data/ folder is private.
It's tricky thing. You should be careful while copying data. Check whether directory of file exist, before writing into them. It's better to explicitly create all files and directories before trying to write to them.
Here is a good example of Database Helper for accessing database. You can modify it so it can use both external and internal storage.

Related

Android Persist Data After Uninstall

I have to persist 2 strings for my application even after the application is uninstalled. Regarding that the end users don't have SD cards for their devices and they don't have internet connection, how could I persist those 2 strings even after the app is uninstalled?
I would highly appreciate any response.
Thanks
Unless you're targeting VERY old phones, you don't need to worry about not having external storage. As long as you use Environment.getExternalStorageDirectory() as your reference, you shouldn't have a problem, though if you're absolutely concerned about this you can check if the external storage doesn't exist and then opt to go to internal storage. Check out this link from the developer docs for a little more insight.
If External truly isn't available, you could then save to Internal memory, but you will have to declare a new permission for that, which may ward off some people.
You have to write it to an SD card/internal storage, and hope the user does not remove that. However, this is a very fragile approach. There is no other solution, as far as I know.
Phones internal storage is also treated as an "SD card". If you create a folder and save it in a text file, it should be safe given user does not manually delete folders after uninstall.
Please check out a section "Saving files that should be shared" in the following web page. Making a file that persists after uninstall entails making it available to other apps/user to read and modify. If those file options aren't intended, you should consider an alternative app design.
http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
After re-install, your app can access the created public directory by using the following function:
public static File getExternalStorageDirectory ()
Regarding the function above, per Google:
Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.
Also, Google recomments placing shared files into a an existing public directory as to not pollute user's root namespace.
Are the strings unique to each user or are they app specific? In either case, the right thing to do would be to save it in some kind of remote server. Firebase is what I use for something like this. Check for its existence in your Application class and download and save it to SQLite if it doesn't exist. For user specific data however, you are going to need some kind of authentication so you know which user is getting what.Firebase does this perfectly well too.
Going by the requirements (no internet, no SD card) of the OP however,I don't see any other way besides one that isn't unethical.

The best place to store a user-accessible configuration file

I have a project consisting of four programs for different platforms; all of them use the same XML-based settings file format. I want to be able to manually modify/overwrite it outside of the application. On Windows, Windows Mobile and Linux I'm using "user.home", but on Android that alias isn't implemented. I'm thinking about simply putting it in the Downloads directory, however, that doesn't feel right.
I can't be the only one, who needs that kind of functionality. Or this isn't Android-way? Any suggestions are appreciated.
EDIT: I'm OK with the settings file not being available all the time (i.e. SD-card removed), it's used only on the start-up of the application.
Store it in getExternalFilesDir(). This would work only if the device has an external storage. The user would be able to access it.
However, take note of the following from the docs:
External files are not always available: they will disappear if the
user mounts the external storage on a computer or removes it. See the
APIs on Environment for information in the storage state.
According to Android data storage documentation you have 5 options:
Shared Preferences. By default this will use file /data/data/your.package.name/preferences/user_preferences.xml
Internal Storage. Here you can use something like /data/data/you.package.name/user.home
External Storage. Similar to internal storage /mnt/sdcard/Android/data/your.package.name/user.home, but if user removes memory card file will be inaccessible.
SQLiteDatabase. You can store the whole user.home file in a database blob.
NetworkConnection. Store user's config in a cloud.

Android permanent storage

I want to create a file and I don't want it to be deleted when the application data is cleared. Is there a way for this in android, something like permanent storage ?
Yes, you can save to external storage (typically the SD card). See the section in the docs entitled Saving files that should be shared:
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.
You can use SQLite for permanent storage. It will do the job perfectly. Avoid using GetExternalFilesDir() if you do not want the data to be deleted when the application removed.
This tutorial from Google will tell you about all options for storage
Data Storage in Android

How to move the database from internal memory to external memory?

I'm storing few sms messages and their details in a database. By default the database is stored in internal memory. I would like to check for the internal memory before accessing the database and once it is full I would like to move the database to external memory. How to handle this?
Pretty much not sure about what you exactly doing up there. Whereas what I do is, there is an app in android market name sms backup and restore. Just run that utility which will save your messages in a xml file in the external storage sd-card. You can move the file wherever you want and xml can be read and write easily too. You can even restore the xml file with the same application to any other android device or into your own device.

App installation on the external storage. What will happen with the database?

My application uses SQL database. What will happen with the location of this database, if I declare android:installLocation="preferExternal" in the AndroidManifest? Will it be also stored on the external database? If no, then how should I handle this in my application? (ideally, if user moved the application, I should also move the database)
The .apk file is saved on the external storage, but all private user data, databases, optimized .dex files, and extracted native code are saved on the internal device memory.
from: http://developer.android.com/guide/appendix/install-location.html
the database will still remain on the internal storage of your phone. you don't have to handle this android will handle it for you
To answer that other question. if you want to bring have your database on the external storage use the SQLiteDatabase.openDatabase method with the path of the database as the paramters. check the following links. Ive found some examples for you take a look at them.
this is a good one!
Is it possible to move the internal DB to the SDCard?
heres another example.
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
u might want to use the documentation as a reference.. Good luck!
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#openDatabase(java.lang.String, android.database.sqlite.SQLiteDatabase.CursorFactory, int, android.database.DatabaseErrorHandler)

Categories

Resources