Data Storage
Data Storage:
There are several ways to store data, ranging from simple key-value pairs to databases.
The choice of data storage method depends on factors such as the type of data, data size, and how the data needs to be accessed and persisted.
common methods of data storage in Android:
Shared Preferences:
For storing simple key-value pairs persistently.
Use Case: Small amounts of primitive data (e.g., user preferences, settings).
API: SharedPreferences.
SharedPreferences preferences = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("username", "JohnDoe");
editor.apply();
Internal Storage:
For storing private data on the device's internal memory.
Use Case: Small files or sensitive data.
API: File I/O (FileInputStream, FileOutputStream).
String data = "Hello, internal storage!";
try (FileOutputStream fos = openFileOutput("myfile.txt", Context.MODE_PRIVATE)) {
fos.write(data.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
External Storage:
For storing public files on the device's external storage (SD card).
Use Case: Large files, media, or files shared with other apps.
API: File I/O (File).
String data = "Hello, external storage!";
File file = new File(Environment.getExternalStorageDirectory(), "myfile.txt");
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write(data.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
SQLite Database:
For storing structured relational data.
Use Case: Larger datasets, complex queries.
API: SQLiteOpenHelper, SQLiteDatabase
// Creating a database and table
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", "John Doe");
values.put("age", 25);
db.insert("users", null, values);
// Querying data
Cursor cursor = db.query("users", new String[]{"name", "age"}, null, null, null, null, null);
Room Database (Android Architecture Components):
A higher-level abstraction over SQLite for easier database management.
Use Case: Recommended for modern Android app development.
API: Room library.
@Entity
public class User {
@PrimaryKey
public int userId;
public String name;
public int age;
}
@Dao
public interface UserDao {
@Insert
void insert(User user);
@Query("SELECT * FROM User")
List getAllUsers();
}
Network Storage (Cloud):
For storing data on remote servers (cloud storage).
Use Case: Shared data between multiple devices, collaboration.
API: Various cloud storage APIs (e.g., Firebase Storage, AWS S3).
// Upload a file to Firebase Storage
StorageReference storageRef = FirebaseStorage.getInstance().getReference();
StorageReference fileRef = storageRef.child("images/myimage.jpg");
fileRef.putFile(Uri.fromFile(new File("path/to/local/file")));
These are some of the common methods for data storage in Android that are used to store values while the application is running. The choice depends on the following criteria like what kind of data will be processed, data volume and how data will be used in the app.