Storing Data on Android

Travis Himes

Introductions

  • Who am I?
  • What's this about?
  • How can I get the most out of this?
  • What are we going to do?

Topics:

  • Shared Preferences
  • Local Files
  • Content Providers
  • Google Drive
  • Parse
  • Firebase
  • Couchbase Mobile

Shared Preferences

  • Key-Value

  • Android-supplied

  • Simple

  • Quick

  • Probably widely understood

Get:

SharedPreferences sPrefs = context
   .getSharedPreferences(
      ACTIVITY_PREFRENCES,       
      Context.MODE_PRIVATE);

Write:

sPrefs
   .edit()
   .putString("CANDY_TYPE", "Butterfinger!")
   .apply();

Read:

sPrefs
   .getString(
      SHARED_PREFS_KEY, 
      "default value")

pros:

  • Simple API
  • Available with Android
  • Same interface (post 11)

cons:

  • Simple objects only
  • Lost with "Clear Data"

Summary:

Local Files

  • Requires Manifest Permission

  • Internal or External

  • Holds more than simple types

  • Device Local

  • Java Devs have likely worked with Files before

  • Risks...

Considerations

  • Internal vs External directory

  • Temporary vs "Permanent"

  • Dealing with File Streams

  • Space concerns

    • Creating

    • Deletion

Internal/"Permanent"

File file = new File(context.getFilesDir(), filename);
-or-
FileOutputStream outputStream = openFileOutput(filename, Context.MODE_PRIVATE);

Temporary

File file = new File(context.getCacheDir(), filename);
-or-
File file = File.createTempFile(fileName, null, context.getCacheDir());

Example

String filename = "myfile";
String string = "Hello world!";
FileOutputStream outputStream;

try {
  outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
  outputStream.write(string.getBytes());
  outputStream.close();
} catch (Exception e) {
  e.printStackTrace();
}

Concerns

  • Buffering
  • Space on Disk
  • File presence
  • All those exceptions

Content Providers

Content Providers, as their name implies, are not great for storing data, they're useful for providing data (in the manner/presentation that you choose) to other applications and processes.

 

Therefore, based on the topic of tonight's talk, I won't go into more detail on them than to make some broad comments...

Off-Device Storage

Google Drive

(Illegitimate)

  • Possible, right now, via forms to store data

    • It's a hack!

  • Not the best move, buuuuut

  • Great for a quick and hackish solution

  • Good for write, didn't have to read, so - have fun...

  • Let me explain you my quick hack!

Steps to duplicate

(don't duplicate)

  • Create a google form
  • Attach it to a google sheet
  • Look at the form's source to get the form id and the column names
  • Write an http POST request to the form with the fields
  • Should show up in the spreadsheet

  • Test this, and never, ever, ship it

  • Seriously, this is a hack, don't ship it

Google Drive

(Legitimate)

  • OAuth setup to allow access

  • Takes Drive space for users

  • File storage (like we talked about earlier)

  • Allows for read and write

  • Google describes the interaction, seriously, it's all files

  • Should be accessible from any device with that account

Parse

So...

  • Parse Server is open-source

  • But, it's a REST API that you're talking with at that point, and that's another whole talk topic...

Firebase

  • Google-backed security

  • File-based

  • Guess what! (hint, I'm getting repetitive)

  • File-based off-device storage - go wild

Couchbase Mobile

  • NoSQL database

  • Key/Value store

  • Synchronizes with the cloud

  • I'm no expert!

  • Requires backend work/agreement

  • Didn't dive too deeply

Questions

Comments

?

Thanks!

@travis_himes

Storing Data

By Travis Himes

Storing Data

  • 1,022