How do you store app data locally using android studio? - java

I'm just getting started in android studio and app development overall. I've done a few tutorials to learn the interface and basics for Android Studio (activities, textviews, buttons, etc) and I've taken a college course in Object Oriented Programming in Java. Now, here's what I'm trying to do:
I'm making a very basic app that's meant to help the user budget their money. The app "acts" as your account and you add the amount of money you've recently been paid. From here, you can allocate your money to savings, checking, and so on. The data I need to store would be the numbers representing dollar values, the different allocations the users made, and the respective dollar amounts to those allocations.
I've managed to contain the data relatively fine within the Java data structures (I developed some algorithms in eclipse to get started). Do I need to serialize this data? Or try writing it to a text file? I've looked around for some tutorials and most low level ones are confusing or require advanced techniques with SQL. I'm interested in simply locally storing just as you would with a to-do list app that keeps record of tasks you entered.
Any and all insight is appreciated.

If I were you I would go the sql route. It's really not hard to get started with sqlite for android. If you haven't already seen Google's own tutorial on the subject, check it out here.
That said though, you can also look at the other methods mentioned here if you feel a database is overkill. Realistically the other choices would be internal storage or external storage if it's something like a todo list. Shared preferences are most often used for persisting user settings.
If you choose to go the file route and don't want to save the data as something like a csv then you need to make your java objects implement the Serializable interface and write them to the file with an ObjectOutputStream.
For example:
FileOutputStream fos = new FileOutputStream("/some/file/path/filename.ser", true);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(yourSerializableObject);

First important thing is that Activities, TextViews, Buttons and other view components are not part of Android Studio. They are part of android framework. Android Studio is IDE as Eclipse, NetBeans and etc.
Regarding your requirement, there are more ways of persisting data in android. You can use files, SharePreferences and sql (sqlite).
Please take a look at following document:
https://developer.android.com/guide/topics/data/
Also i would suggest you to follow Google android official documentation.
https://developer.android.com/guide/
Best Regards,
Milan

There are several ways to store your data locally on the device:
SharedPreferences - usually they are used to store app settings (Such as first name, last name, etc.).
Databases - used to store structured data, best solution when app needs to store large amount of data. Also, thay has powerful and flexible mechanisms of read/write operations.
Files - Not the best way to save structured data, no build-in mechanisms. Also app needs additional permissions to write/read operations (if you decides to save data on external storage), which can be denied by user(if your app is targeting api 23 and greater you should use runtime permissions)
The most effective way to store app data in your case is databases.
Google has developed Room Persistence library, which provides abstraction level over SQlite and makes all things simple and easy to understand.
Here is a nice example on Medium, which provides easy to understand tutorial for Room Persistence library.

Related

Android: Should I use local Storage or SQLite (or similar)?

I am just starting to learn Android Development. I have to develop something similar to a Log APP where the user can save information.This is student project, so I don't have to use Users/Passwords, instead I am allowed to save the info using the Local Storage of the device.
The problem is that I will need to extract that info into a CSV file, and I am not sure if using Local Storage is the best option.
Which option do you think is more efficient?
Which one do you think is less complicated to use in this case?
Thanks in advance!
You probably wanted to use Room for it is the official Android Database framework and the learning curve is quite easy. It even supports other Jetpack libraries such LiveData to make your Android development far more easier.
Use SQLite. You can query the data from the database and write it to a cvs file upon need.
Definitely use Sqlite, and not raw sqlite but use instead Room. Room is architecture component by android and is actually just one layer above sqlite so it is easier to use and removes a lot of boilerplate code. For storing really little things like login information you can use SharedPrefrences. Here is documentation about Room I suggest reading it.

Saving Things to local storage in java/javafx?

I don't know if this is a super noob question, but I'm currently building an app from scratch with the knowledge i've gained so far, and I'm making a "Notes" app where You can create notes, delete, edit(update), etc etc.
The question I'm having is that I know I can use a database to store data from my input, and I know i can use A Database to save, open the data, but Just incase, does Java have a LocalStorage, like JavaScript/JSON where you can save things and open them up later?
If there is LocalStorage for applications to save data, and open it up after running again, what would be some good resources to read about it and learn it so I may integrate it into my application?
If there is no localstorage, I wan't to be able to perhaps allow the user to add images when creating notes, how would I be able to do that using a database?
As far that I know of there packages that allow you to store data as you intend, but following on your next question, you could possibly store the address/file name of the image you wants to add to the database. You could then use it to retrieve the image file and display it.
If you have any questions on image implementing images in java, reference this documentation and tutorial
If you have any more questions on working with databases, reference this documentation
I hope this helps.

Where to place my android app's data

I'm currently developing an app that lets you create and save excercises and routines for the gym. I want the app to come with some example excercises and routines and also to give the user the ability to add his own.
The thing is, I know I can use "getFilesDir()" and save it in the internal memory, but if I do that, how do I put my example files also in the internal memory? I don't think I can do that without creating those files by code everytime the app runs. I've also tried putting the example excercises in "res/raw" but then the ones the user adds go to a different place and I don`t know if that's good practice, apart from just how annoying it is having to read them from different places.
Where would be the best place to put those excercises and routines files?
Thank you very much!
The best practice is to store data inside "Sqlite Database".
"Sqlite Database" is the internal database that android provides to store data locally and is easy to use.
It's queries are easy to implement. It is more easy to work on, if you have worked on any database before. Just create a "Database Helper" class and initiate inside the activity where you plan to store data.
Many big applications like "whatsapp", use this database to store data on user's device.
The plus point of using "Sqlite" is that, you can iterate through each and every data easily.
It is quick, easy to work and is also a good programming practice. It is also secure.
While using a sqlite database for managing your app data is the traditional
approach, there are also alternatives to it. Realm is such an alternative. You can check the comparison with sqlite and see if it meets your need.
https://realm.io/
In Android development, you can store locally and as well as remotely. This link will walk you through all possible ways to store data.
As per your requirement, I would recommend you got for SQLite Database provided especially for android as it is light weight. Sqlite queries are straightforward and easy to use with some APIs comes with the package. you can start with this link with Sqlite.
I suggest using Firebase to store your data. Not only it is online and realtime, it can also work in offline mode and sync later. Because you're developing a gym app, why not give it an online or offline capability? I think users prefer it that way. You can check it at firebase.google.com

How to save files on android storage, write to them, and access them later on?

I am working on an android app to take float values from 3 different arrays, transform them into strings and then store them into a text file on the internal storage of the phone. I want to be able to access these values later and put them on a computer so I can do some statistical analysis on them. Can someone please show me how to do this?
Create a File object, and store your values in a flat-file database. For more info on how to create a flat-file database, go here. To learn more about the File API, go here. To learn about more data storage options that Android provides, go here.

Android Open and Save String to/from Google Drive SDK

I have read allot about Google drive integration and I want to save STRING into Google drive and also to retrieve, I have used this demo but it is giving me errors.
please anyone provide me demo for my requirement
First, let's clear the confusion here. Your original question refers to a demo that uses the original RESTful API (aka DriveSDKv2), where Simple Plan mentions a different API, the new Google Drive Android API (GDAA).
The GDAA, does not have the full functionality at the moment, but it can be combined with the DriveSDKv2 to supplement what's missing (delete, description, indexable, ...). On the other hand, the GDAA takes care of all synchronization, offline and caching issues. You just create, retrieve, update the files as if they were local and the magic of up/down synchronization is left to GDAA. And the missing functionality is promised soon.
If you decide to dive into GDAA, there are 3 demos available on Github at the moment:
quick start
full-blown demo
trivial example with wrapper class to create, retrieve files/folders
Both demo and trivial examples have the create functionality with a text written to a file. If you pull any of them and step through, you'll get the idea.

Categories

Resources