Methods for saving user input like notes (Android Studio/Java programming)? - java

I am starting to code an app for taking notes, like Evernote or a basic preinstalled memo app, so that I can learn and practice coding.
Currently I save the user input in a .txt file, so that every note would have an own text file in the storage, with the note content.
What are other methods of saving user input in storage (you don't need to explain it, keyword would be appropriate) and what are the advantages or disadvantages of doing so? What can be cons of saving text files like I'm now doing?

Save the content to a file in your app's cache
If the content is plain text (and not too long), you can easily use SharedPreferences to save the content
You can use a database
Note that if the content is rich text, you can format that (for example, using HTML, JSON or XML and save files (like images) in a specified folder and write the location of the files to the formatted text) and then save to a database.
Useful links to get started:
Using databases:
https://developer.android.com/training/data-storage/sqlite
https://developer.android.com/training/data-storage/room
https://www.tutorialspoint.com/android/android_sqlite_database.htm
Rich Text Editors:
https://github.com/chinalwb/Android-Rich-text-Editor
https://github.com/wasabeef/richeditor-android
How to get cache directory?
File cacheDir = this.getCacheDir();
or
File cacheDir = this.getApplicationContext().getCacheDir();
Note that if the content is important, you can create a new folder in the storage (like "My App Name Files") and save the content to that folder.
If you are using EditText:
I name the EditText uinput. Here we go:
private void saveContent() {
String content = uinput.getText().toString();
String name = "Note 1"; // You can create a new EditText for getting name
// Using SharedPreferences (the simple way)
SharedPreferences sp = this.getApplicationContext().getSharedPreferences("notes", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString(name, content);
editor.apply();
}
private Map<String, ?> getAllNotes() {
SharedPreferences sp = this.getApplicationContext().getSharedPreferences("notes", Context.MODE_PRIVATE);
return sp.getAll();
}
private String getNoteContent(String noteName) {
SharedPreferences sp = this.getApplicationContext().getSharedPreferences("notes", Context.MODE_PRIVATE);
return sp.getString(noteName, "Default Value (If not exists)");
}
Don't save other things in SharedPreferences "notes".

Related

Can i store some string value into a text file in R.drawable folder in android

I am making a lock screen application. In my application, i want to store a PIN.. But every time when my activity called from the service, The PIN value is being resetted. So i want to store the value of PIN in some permanent place. Is there any way to store the PIN to a text file in R.drawable ? or is there any better ways ? Please help me
Yeah you don't want to put it in the resource folder. Use SharedPreferences instead.
To save a value:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putString("pin",your_pin).apply();
To read a value:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String pin = prefs.getString("pin","default pin");
Is there any way to store the PIN to a text file in R.drawable
No not possible to write or change files in any folder which bundle with apk.
is there any better ways ?
Use following ways to store data in application:
1. Use SharedPreferences for saving data
2. Create file for saving data in internal directory for your app using getFilesDir()
3. Create file for saving data in internal directory for your app's temporary cache files using getCacheDir()
NOTE : all files created using above methods will delete when application un-install or cache is clear from Application Manager.

How to add a file automatically when I install my android application on a device?

I m developing an application. My application read some config parameters from: /data/data/package_name/shared_prefs.
I have to create this file manually each time I install the application on a new deice. How I can make the file created automatically when I install my android application
In your first Activity(MainActivity) you need to add this code to create Shared Preference file
String MY_PREFS_NAME = "FileName";
int MODE_PRIVATE = 0; //Zero means private mode
Sting strStore ="some string to store in file"
int value = 6;//integer value to store in file
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME,
MODE_PRIVATE).edit();
editor.putString("Storestr", strStore);
editor.putInt("intvalue", value);
editor.commit();
To get the Shared Preference file value
SharedPreferences GetValues = getSharedPreferences(MY_PREFS_NAME , MODE_PRIVATE);
String strGETSPStore = GetValues.getString("Storestr", "");
int GETSPvalue = GetValues.getInt("intvalue", 0); //Zero is default value
The file will store in the path:
/data/data/package_name/shared_prefs/FileName.xml , File will be created automatically when app is started.
And Shared preference file is an .xml file.
If File already exists it will re-write the file when you start your application again.

Working with files Android programming

I need your help.
So, what i intend to do is to read a value from a file and to increase this value by one, then write it in the same file, i don`t know where is my error.
This is the procedure which i use to do all of this :
And together with the folders :
I really appreciate your help until now, would be really thankful for any advice.
The problem i am facing i can`t find the file on device, the text file should be a permanent file created on the device in order to store some data, even if the app is closed or the phone is switched off.
The problem is that you in android you can't access file like you did with direct path
new File("./data/text2.txt");
You can use the Eniornment class to get the path to data folder than here create the your file and save the values.
For example :
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
File path = Environment.getDataDirectory();
File file = new File(path, "text2.txt");
// from here you read and write like you already did.
}
However if you only need to write an Integer you can use SheredPreferences like this:
//get the value
SharedPreferences settings = getSharedPreferences("prefs", 0);
int test = settings.getInt("test", -1);
test++;
SharedPreferences.Editor editor = settings.edit();
editor.putInt("test", test);
editor.apply();

Android Store and Retrieve Data

How do I store and retrieve private application data for my app? I have an activity that runs depending on certain parameters entered by the user (file path, volume, etc.) and I want to be able to store what they input and then retrieve it every time they open the activity.
Should I user FileWrite or FileOutputStream/FileInputStream?
UPDATE:
Currently I have this as the code for saving the string.
final String audioPathFN = "Audio_Path";
final String audioPathC = String.valueOf(path);
FileOutputStream saveAudioPath;
try {
saveAudioPath = openFileOutput(audioPathFN, Context.MODE_PRIVATE);
saveAudioPath.write(audioPathC.getBytes());
saveAudioPath.close();
} catch (Exception e) {
e.printStackTrace();
}
Now how do I retrieve the string so I can use it in the app?
There are a few mechanisms to store private data :
SharedPreferences
using a simple file in you activity.getCacheDir() folder
a database
All those options are explained here : http://developer.android.com/training/basics/data-storage/index.html
Of course, all those solutions are local, you can also wanna use a web-service to communicate data with a server, but you would then be entering in a totally different landscape.

Save images and Strings in exit

i want to save a List of images and list of String when i exit from my app (when i will open my app again i will can manipulate them)
i didnt find how can i implement it.
i only found that i can save my detailes with this:
SharedPreferences myPrefs = getSharedPreferences("myPrefs", MODE_PRIVATE);
SharedPreferences.Editor e = myPrefs.edit();
e.putString("pathreturned", path);
e.commit();
however i didnt find how can i save list (of Bitmap or String) or photo?
thanks alot
How to save images in Android? This discusses not to SD card and suggests that you save files to a SQLite database as BLOB's.
Alternatively, Save image to sdcard from drawble resource on Android highlights how to save it to the SD card.
you can do this but you have to save the image on the sd card and you can use it as below...
SharedPreferences mypref = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit=mypref.edit();
edit.putString("imagepath","/sdcard/imagename.jpeg");
edit.commit();

Categories