Android's Backup Manager to backup a minimal file - java

is it possible to use
Android's Backup Manager
to backup a minimal file
(A text file with 20 words?)
I can't quite understand if this is possible and how to do it. ..

As you can read at http://developer.android.com/guide/topics/data/backup.html you can save any type of data, including files.
Also take a look at http://developer.android.com/reference/android/app/backup/FileBackupHelper.html

If you are using the original backup APIs, you probably just want your BackupAgent implementation to extend android.app.backup.BackupAgentHelper, and then use a FileBackupHelper configured for the file you want to keep backed up.
https://android.googlesource.com/platform/development/+/master/samples/BackupRestore/src/com/example/android/backuprestore/FileHelperExampleAgent.java is an example of using FileBackupHelper for a single file. There are other example agents in that same sample code directory that show other alternatives for keeping data backed up.
On Android 6.0 (Marshmallow) and later, there is a new file-based backup API you can use instead that requires much less code in your application, possibly none at all. Take a look at http://developer.android.com/training/backup/autosyncapi.html for an overview of how you can use this new facility to get automatic backup of a file without having to actually write your own backup agent.

Related

Android - Best way to edit .txt file?

I've had a look around and haven't been able to come up with an answer to my issue.
I'm creating a fitness app and it allows users to save different workouts for future use. All the information is saved in txt file on internal storage. I'm trying to now implement a feature to be able to edit these workouts, so my question is:
What is the best way to edit a .txt file in android?
Should I just delete the old entry and save the new one in its place or is there a better way?
You can only append to a text file (add to the end); any other edit requires that you load the full file in working memory, modify it, and save it a new file (possibly overwriting the old one).
If this sounds like a bad idea (because the files are large and complex) then perhaps you should be looking at using SQLLite facilities which are standard android libraries and designed relatively simple record keeping tasks of this nature.
Unless your data is extremely unusual the SQL path will make for easier, clearer code in the long term.
Using SQLite database maybe the best way of saving data for different users. And if you want to edit a text file, you can load the full file in memory and rewrite the file after modify the content.

How to save application data for use across users in OS X

I have a Mac Java application that needs to persist data across reboots. The data needs to be saved so that any user has access to it. E.g. an SQLite database file that can be used for all users.
It looks like the /Library/Application Support/ folder is supposed to be used for this, but I'm not able to write to it without making my app run as root or changing the permissions of the file to rwxrwxrwx.
What is the proper way to save application-level data on Mac?
The developer documentation covering this is a bit of a large topic:
https://developer.apple.com/library/Mac/referencelibrary/GettingStarted/GS_DataManagement_MacOSX/_index.html#//apple_ref/doc/uid/TP40009046
https://developer.apple.com/library/mac/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40010672
According to the File System Programming guide you should make a specific subdirectory inside /Library/Application\ Support for your app to store app data common to all users on the system. I'd use reverse domain name notation such as com.yourcompany.yourapp or something else unlikely to collide with another app's use of the common directory for this.
You might also look into using an existing app bundler for OS X such as https://bitbucket.org/infinitekind/appbundler rather than hard code paths to file locations.

notepad eclipse java android internal storage

I am trying to do an eclipse java android app using internal storage.
It is a notepad. What it should be able to do is write and save notes, search for them an edit them.
the most similar application of what I need is this:
http://developer.android.com/training/notepad/notepad-ex3.html
but I dont know how to modify if to use internal storage and not external, i also found this file that allows you to read and save in internal storage
http://www.androidaspect.com/2013/09/android-internal-storage-tutorial.html
but now i dont know how change the database for the inernal storage
Okay let's break it down. You want to make something that will let you take notes. Fair enough. You need the kind of storage that will make retrieval and insertion easier.
Let's start with a single text file that you store somewhere (internal or external). If you use this approach, inserting and deleting data becomes difficult because any changes will require you to make changes to the actual text. Not to mention, you will need to format your notes in a way so that you can differentiate one note from another.
Okay, so will using multiple text files solve this? In a way, yes, but you can go for a better approach using SQLite.
SQLiteDatabase has methods to create, delete, execute SQL commands,
and perform other common database management tasks.
SQLite seems like the prime candidate for your storage because it makes CRUD (Create, Read, Update, Delete) easier. Instead of trying to modify the code, start from scratch.
Have a look at this tutorial https://www.youtube.com/watch?v=j-IV87qQ00M

Open and edit file in .jar with Java?

How would you go about opening an .xml file that is within a .jar and edit it?
I know that you can do...
InputStream myStream = this.getClass().getResourceAsStream("xmlData.xml");
But how would you open the xmlData.xml, edit the file, and save it in the .jar? I would find this useful to know and don't want to edit a file outside of the .jar... and the application needs to stay running the entire time!
Thank you!
Jar files are just .zip files with different file suffix, and naming convention for contents. So use classes from under java.util.zip to read and/or write contents.
Modifying contents is not guaranteed (or even likely) to effect running system, as class loader may cache contents as it sees fit.
So it might be good to know more about what you are actually trying to achieve with this. Modifying contents of a jar on-the-fly sounds like complicated and error-prone approach...
If you app. has a GUI and you have access to a web site/server, JWS might be the answer. The JNLP API that is available to JWS apps. provides services such as the PersistenceService. Here is a small demo. of the PersistenceService.
The idea would be to check for the XML in the JWS persistence store. If it is not there, write it there, otherwise use the cached version. If it changes, write a new version to the store.
The demo. writes to the store at shut-down, and reads at start-up. But there is no reason it could not be called by a menu item, timer etc.

How to get File within' package, in an Android application

I'm am creating an Android application, but in order to have one of the functionalities working I need to read a predefined xml file whilst only knowing its name, not the R.id..
In normal Java I know I can use
getClass().getClassLoader().getResource(xmlName)
but using the limited Android SDK thats not working, any knows how to solve this?
Use getResources().getIdentifier() from your Context (e.g., Activity), but please cache the result if you will use it more than once. getIdentifier() is implemented on Resources.
From the Data Storage Section in the android developer manual:
If you have a static file to package with your application at compile time, you can save
the file in your project in res/raw/myDataFile, and then open it with
Resources.openRawResource (R.raw.myDataFile). It returns an InputStream object that you can
use to read from the file.

Categories