Android permanent storage - android

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

Related

Can anyone tell me about InternalStorage in Android ? I am confused

I am new to android . I have little confusion on internal storage topic in android. Please correct me if i am worng
InternalStorage :- Allows us to read and write file that are associated with each application's internal memory so the data are private to your app.
This file can only be accessed by the application and cannot be accessed by other application or users
Is it true that InternalStorage store data in device internal memory ?
If answer is yes then how can i store images ,videos etc that user can see and access it on device or by pc when they are connected through usb?
if my app downloads an image and device does not have sdcard and i have used internalstorage method in this condition how user can see dowloaded image using usb or from device ?
It will be good if explanation is given with example .Thanks in advance
You must save your files on external storage. Notice that external storage does not automatically mean removable storage. Normally the devices built-in storage is divided into internal and external storage. However, external storage also includes removable storage like SD cards.
So, if you want to save files that the user should be able to access for example with his PC you should save to the external storage.
The respective user guides on that topic are in particular:
https://developer.android.com/training/basics/data-storage/files.html#InternalVsExternalStorage
https://developer.android.com/guide/topics/data/data-storage.html
For example, if you want to save a picture that is accessible by the user in the expected directory you would use
File picture = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "filename.png");
Since API level 17, MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE are deprecated in internal storage method.so, you cannot write a file that can be accessed by user using internal storage method.
try External Storage to write file so that you can access.for more
https://developer.android.com/guide/topics/data/data-storage.html

Is there a way to see what data stores each app in phone memory/on sd card?

I want to track everything that apps store in phone memory/on sd card, so I can delete it if the app is uninstalled. Is this possible? If not, how can I get all the files from phone memory/sd card?
there is no way to "track" what your application creates, i think..
generally, the app stores data in the Cache directory, which is :
"<storage directory>/data/<your package name>/"
on a device which supports external storage, the "storage directory" is generally "/mnt/sdcard"
you can read the documentation to learn how to use the External storage to save files you create through your application.
I'd also like to point out that there is no way for you to know when your app is being uninstalled, so you can't delete the files before uninstall manually anyway,if that's what you intend to do. The android system itself removes the files stored in your cache directory, though.

Is it a good practice to copy database from SD card in application itself once the application starts?

I don't want the SD Card to be removed by user when my application is running, as it will contain the database I would be using in my application for displaying images and information from it.
Possible solutions as I see can be to copy the contents of the Database in SD card into the application at the start of the application (but it might consume time).
Is there any particular way by which I can freeze my application when the SD-Card is removed ?
Thanks for your help in advance !
Cheers,
Sumit
How about don't have the database on the SD card in the first place? If you are downloading it, download it directly to internal storage. If it is inside the APK as an asset or resource, extract it to internal storage.
If you need to use external storage (SD card), check if external storage is mounted when the app starts. If it is not, display an appropriate message and finish the activity.

Is it possible to create shared preference files in sdcard

Can anyone help me ? Is it possible to create shared preference files in sdcard rather than on private path of an android application.
You cannot modify where shared preferences are stored.Its a private storage. if you want to use sd card use
Environment.getExternalStorageDirectory(). and get the path of directories stored in sdcard.
it is possible by reading&writing the xml file into the external storage ,but it's not the same as sharedPreferences. you will have to implement your own way , or use the android code of this class.
however , there are some disadvantage over using the internal storage:
anything you store on the sdcard is visible to every application and the end user can read it by just opening it.
only in the internal storage you get some sort of protection against reading the file , so that only rooted devices can read the files.
external storage can also be unmounted , so the data can sometimes be unreachable. you need to handle possible errors that can occur because of this.
uninstalling the app while the sd card is mounted means that the data will stay on the sdcard .

Android save app settings/data in Internal/External Storage

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.

Categories

Resources