Posted on Leave a comment

Data storage

  • file storage
  • SharedPreferences
  • SQLite database

FileStorage

  • internal – private
  • external – public
Internal

Access mode by default is MODE_PRIVATE

Before Android 7.0 also: MODE_APPEND, MODE_WORLD_READABLE, MODE_WORLD_WRITEABLE

Since Android 7.0 we should use FileProvider

Files will be deleted if you delete a app.

Methods:

  • mContext.getFilesDir() – to get private dir in storage
  • mContext.getCacheDir() – to get private dir for save temp files. The files can be deleted by OS, but a developer have to delete these files by himself.
External
Public storage of app

example: Android/data/data/your_package

Files will be deleted if you delete a app.

The files is accessable to all apps.

[code language="java"]
mContext.getExternalFilesDir(), mContext.getExternalCacheDir()
[/code]
Public storage of somedata

example: Pictures/your_app

Files will NOT be deleted if you delete a app.

[code language="java"]
Environment.getExternalStoragePublicDirectory()
[/code]
flashcard

example: mnt/sdcard/Android/data/data/your_package/mnt/sdcard/Pictures/your_application/


There are some content dependent types of directory

[code language="java"]
mContext.getExternalFilesDir(String type)
Environment.getExternalStoragePublicDirectory(String type)
[/code]

DIRECTORY_MUSIC
DIRECTORY_PODCASTS
DIRECTORY_RINGTONES
DIRECTORY_ALARMS
DIRECTORY_NOTIFICATIONS
DIRECTORY_PICTURES
DIRECTORY_MOVIES
DIRECTORY_DOWNLOADS
DIRECTORY_DCIM
DIRECTORY_DOCUMENTS

Read/write

[code]
<manifest ...>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
[/code]

Read only

[code]
<manifest ...>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>
[/code]

Since 4.4 it is not required to ask permissions if you use only public storage of app Android/data/data/your_package/

Since 7.0 we can ask access to certain public dirs (Scoped Directory Access)

SharedPreferences

it is a file of key-value
key – string
value – boolean, int, long, float, string

[code language="java"]
SharedPreferences mPref = mContext.getSharedPreferences(“file_name”, Context.MODE_PRIVATE);
[/code]

– common options, available fron any components of the app

[code language="java"]
SharedPreferences mPref = mActivity.getPreferences(Context.MODE_PRIVATE);
[/code]

– options of specific activity, available fron any components of the app

SQLite

  • full sql
  • onefile database
  • opensource
  • max size – 140 Tb
  • max size of string – 1 Gb
  • ACID transaction

data type:

  • null
  • integer
  • real
  • text
  • blob
[code language="java"]
public final class FeedReaderContract {
private FeedReaderContract() {}

public static class FeedEntry implements BaseColumns {
        public static final String TABLE_NAME = "entry";
        public static final String COLUMN_NAME_TITLE = "title";
        public static final String COLUMN_NAME_SUBTITLE = "subtitle";
      }

private static final String SQL_CREATE_ENTRIES =
    "CREATE TABLE " + FeedEntry.TABLE_NAME + " (" +
    FeedEntry._ID + " INTEGER PRIMARY KEY," +
    FeedEntry.COLUMN_NAME_TITLE + " TEXT," +
    FeedEntry.COLUMN_NAME_SUBTITLE + " TEXT)";

private static final String SQL_DELETE_ENTRIES =
    "DROP TABLE IF EXISTS " + FeedEntry.TABLE_NAME;
}
[/code]
SQLiteOpenHelper
[code language="java"]
public class FeedReaderDbHelper extends SQLiteOpenHelper {
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "FeedReader.db";

    public FeedReaderDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    public void onCreate(SQLiteDatabase db) {
  // Do update database
        db.execSQL(SQL_CREATE_ENTRIES);
    }
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  // Do update database
    }
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  // Do update database
    }
}
[/code]

Create

[code language="java"]
// Obtain connection for writing
SQLiteDatabase db = mDbHelper.getWritableDatabase();

// Create a new map of values, where column names are the keys
ContentValues values = new ContentValues();
values.put(FeedEntry.COLUMN_NAME_TITLE, title);
values.put(FeedEntry.COLUMN_NAME_SUBTITLE, subtitle);

// Issue SQL ‘insert’ statement
long newRowId = db.insert(FeedEntry.TABLE_NAME, null, values);
[/code]

Read

[code language="java"]
// Obtain connection for reading
SQLiteDatabase db = mDbHelper.getReadableDatabase();

// The columns to return
String[] projection = {
    FeedEntry._ID,
    FeedEntry.COLUMN_NAME_TITLE,
    FeedEntry.COLUMN_NAME_SUBTITLE};

// The columns for the WHERE clause
String selection = FeedEntry.COLUMN_NAME_TITLE + " = ?";

// The values for the WHERE clause
String[] selectionArgs = { "My Title" };

// The sort order
String sortOrder = FeedEntry.COLUMN_NAME_SUBTITLE + " DESC";

// Issue SQL ‘query’ statement
Cursor cursor = db.query(FeedEntry.TABLE_NAME, projection, selection, selectionArgs,
null, null, sortOrder);

// cursor
List itemIds = new ArrayList<>();

// loop
while(cursor.moveToNext()) {

  // get value
  long itemId = cursor.getLong(
      cursor.getColumnIndexOrThrow(FeedEntry._ID));
  itemIds.add(itemId);
}

cursor.close();
[/code]

Update

[code language="java"]
// Obtain connection for writing
SQLiteDatabase db = mDbHelper.getWritableDatabase();

// Create a new map of values, where column names are the keys
ContentValues values = new ContentValues();
values.put(FeedEntry.COLUMN_NAME_TITLE, title);
values.put(FeedEntry.COLUMN_NAME_SUBTITLE, subtitle);

// The columns for the WHERE clause
String selection = FeedEntry.COLUMN_NAME_TITLE + " LIKE ?";

// The values for the WHERE clause
String[] selectionArgs = { "MyTitle" };

// Issue SQL ‘update’ statement
long rowCountAffected = db.update(FeedEntry.TABLE_NAME, values, selection, selectionArgs)
[/code]

Delete

[code language="java"]
// Obtain connection for writing
SQLiteDatabase db = mDbHelper.getWritableDatabase();

// The columns for the WHERE clause
String selection = FeedEntry.COLUMN_NAME_TITLE + " LIKE ?";

// The values for the WHERE clause
String[] selectionArgs = { "MyTitle" };

// Issue SQL delete statement
long rowCountAffected = db.delete(FeedEntry.TABLE_NAME, selection, selectionArgs);
[/code]
Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.