I am working on logs inside android, I thought two ways for storing logs, and one is on external directory as a text file or a log file while other is to store in database. I found database method more useful in my case. My question is if I UN-install and reinstall the app will the database will be affected? In case of yes what should I do? I cannot place the logs online. How to take the backup or safe that database so it won’t be affected in case of UN- installation.
My question is if I uninstall and reinstall the app will the database will be affected?
Yes, your database will by default be stored in the application's data directory, which is deleted along with your application on uninstallation.
You can instead write a file of a filetype of your choosing (whether that's a simple text file, or a database file) to the external storage. You can obtain the directory path using:
Environment.getExternalStoragePublicDirectory(type) for obtaining a directory path of files of a specific type, such as images or videos;
Environment.getExternalStorageDirectory() for obtaining the primary external storage directory, under which you could create a new path.
I would nevertheless discourage you from doing this, because it would require your users to manually dispose of any files after uninstalling your application. Perhaps you should reconsider the justification of choosing for this solution.
If you uninstall the app, then the database (and all other data stored in the apps private storage for that matter) will be gone.
You could store your logs in the public external storage, but this will expose your logs to other applications as well as the user.
One possible approach could be to use application private storage for your 'live' logs, and make periodic backups to the public storage. In case of a new installation, you can check your designated backup location and attempt to restore previous logs from the backup.
Related
I'm currently building a .Net Maui App that is only targeting android. I need to save some data in the public external storage that I can access and copy to my PC and that persists even if I uninstall the app. I choose the Documents directory.
The following does the job:
string dir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDocuments).AbsolutePath
But I get a warning from my IDE that GetExternalStoragePublicDirectory is deprecated.
I've found several postings that
Android.App.Application.Context.GetExternalFilesDir(Android.OS.Environment.DirectoryDocuments)
will also work, but that does not return the same result.
The deprecated method returns
/storage/emulated/0/Documents
while the other one returns
/storage/emulated/0/Android/data/com.companyname.myappname/files/Documents
Hence the appname is in the path, this is not persistent in case the app is uninstalled. So what is the correct way to get the public external documents directory?
I had done a sample to test the MediaStore and the Context api. According to your description, you want to save some data to the file in the Document folder.
For the Context
All the files or directories you get from it belong to the application, when user uninstall the app from the devices, all of these will be deleted from the device. So this doesn't meet the requirement.
For the MediaStore
This api can be used to create file in the public folder, but the file can just be accessed by the app which create it. When user uninstall the application, you can't access it either.
And the google suggest the developer use the ACTION_CREATE_DOCUMENT to let the user create a file by himself and then user can open it by the ACTION_OPEN_DOCUMENT.
So even though the Android.OS.Environment.GetExternalStoragePublicDirectory method has marked as deprecated, it is the easier way to get the result you want.
For more information, you can check this case and the official document.
As we all know starting Android Q (API=29) introduced new Scoped Storage concept, which basically prevents access to external files/dirs, except media files. Namely:
Programmer can use internal/app specific storage (no limits) - getFilesDir()
The only external storage can be accessed is via getExternalFilesDir(), but during app uninstall data will be deleted
There's option to access external storage via MediaStore, but it works only for media files
App still can read any external file via: getContentResolver().openInputStream(uri)
In my case I have app, which stores user data in SQLite database in app specific directory, database size can be up-to several gigs.
In all previous versions of my app users used to backup data to external storage/SD/USB and transfer those data to their new device, so they could continue to use data (it's encrypted database containing any kind of private data, including audio/video/documents and so on).
Question: how can I backup this database? I couldn't manage to find appropriate way to save/backup data to any external storage...
P.S. Android autobackup service is unworkable due to the size of database.
I'm developing an application which will do the backup of Application's database to the Google servers and restore the data after reinstalling the application.
I didn't find any reference to do this task. I also tried the application given in Android SDK, which is working only with the Shell commands, but not by running the application.
Could anybody is having an idea how to do the backup and restore of the Application's database data?
Android has a "backup data" service that can help you in this particular case. You can find documentation here. I think it can solve your problem, but I didn't use it, so I can't describe the process.
You have also a good question in StackOverflow about backup/restore an SQLite internal DB. Just have a look at this link.
SQLite's backup API works only where the backup is another file that can be accessed directly (for example, if you want to create a backup on the SD card), so it cannot be used when the backup goes "into the cloud".
Ensure that there is no active transaction, then backup the database file just as you would backup any other file.
I want to store some data to a File (not database) on the Android device. Currently I'm using Context.getFileDir() as the parent directory. However, when you update the app that directory gets deleted. Is there a directory I can use from Context that won't get wacked when the user goes to update the application? Does it have to be the SD Card since that's not always generally available on all phones?
However, when you update the app that directly gets deleted.
No, it doesn't.
Is there a directory I can use from Context that won't get wacked when the user goes to update the application?
No files ever "get wacked when the user goes to update the application".
All files in the on-board flash will "get wacked when the user" uninstalls the app.
Does it have to be the SD Card since that's not always generally available on all phones?
External storage is "generally available" on all Android devices that have the Android Market. It might not be available in specific circumstances (e.g., it is mounted on a host PC, external storage was removable and was actually removed).
If you want to file not to be deleted on uninstall is not possible as you might know that uninstall will delete all the data.
While if you want to save the file on update of code you can use that same method as you are using by creating file on getFileDir(); just you have to check out each time before creating file that if file already exists or not.
If file exists there is no need to create again and if it is not there then create it.
I am assuming that you have done all stuff of file creating properly. Just add below code before creating it.
if(f.exists()) //Where f is your file
{
//Don't create the file, it already exists
}
else
{
//Create the file, since it didn't exist
}
I have a lite version of an application that uses a SQLite database. I want to copy that database over to the full version of the application when the user installs the full version.
I have written some code to perform the file copy, but the lite database file always comes up as unreadable. The file is there and I can point to it, but I can't read it to perform the copy.
In the Android documentation, we read:
You can save files directly on the
device's internal storage. By default,
files saved to the internal storage
are private to your application and
other applications cannot access them
(nor can the user).
Note the words, "by default".
Is there a way that I can override that default and make the SQLite file readable by my other application?
Thank you.
I believe you have 2 options.
Set the sql database to be world readable on creation. You can do this by setting the appropriate mode parameter in the call to openFileOutput() or openOrCreateDatabase().
Set the sharedUserId attribute in the manifest of both of your applications so that they have the same user ID. This treats both applications as the same user, giving both applications access to the same private set of files.