Created
April 13, 2015 20:18
-
-
Save dnkm/d49db385994f6439766e to your computer and use it in GitHub Desktop.
Android Tutorial - Chapter 5 Saving Data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// use SharedPreferences APIs for small collection of data | |
// 1. using key | |
SharedPreferences pref = anyContext.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE); | |
// 2. activity's default | |
SharedPreferences pref = getAcitivy().getPreferences(Copntext.MODE_PRIVATE); | |
// Write | |
SharedPreferences.Editor editor = pref.edit(); | |
editor.putInt(getString(R.string.saved_high_score), highScore); | |
editor.commit(); | |
// Read | |
long highScore = pref.getInt(getString(R.string.saved_high_score), defaultValue); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
myFile.delete(); | |
myContext.deleteFile(fileName); // internal storage | |
// internal storage - deleted with uninstall | |
// getExternalFilesDir - deleted with uninstall | |
// getExternalStoragePublicDirectory - kept | |
// getCacheDir - delete on a regular basis |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<manifest> | |
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | |
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /><!-- if only read is needed --> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// internal storage - always available. belongs to app. | |
// external storage - not always available. world readable | |
// change [android:installLocation] settings to allow installation on external storage | |
// easy way | |
File file = new File(context.getFielsDir(), filename); | |
File tempFile = new File(context.getCacheDir(), filename); // system may delete without warning | |
File photo = new File(Environment.getExternalFilesDir(Environment.DIRECTORY_PICTURES), albumname); // uninstall-cascade | |
File photo = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), albumname); // uninstall-safe | |
// another way | |
FileOutputStream os; | |
try { | |
os = openFileOutput(filename, Context.MODE_PRIVATE); | |
os.write(string.getBytes()); | |
os.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
// cache | |
try { | |
File file = File.createTempFile(filename, null, context.getCacheDir()); | |
} catch (IOException e) { | |
// error creating file | |
} | |
// availability of external storage | |
String state = Environment.getExternalStorageState(); | |
if (Environment.MEDIA_MOUNTED.equals(state) {} // writable? | |
if (Environment.MEDIA_MOUNTED.equals(state) || Environemtn.MEDIA_MOUNTED_READ_ONLY.equals(state)) {} // readable? | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class FeedReaderContract { | |
public FeedReaderContract() {} // prevent accidental instantiation | |
public static abstract class FeedEntry implements BaseColumns { | |
public static final String TABLE_NAME = "entry"; | |
public static final String COLUMN_NAME_ENTRY_ID = "entryid"; | |
public static final String COLUMN_NAME_TITLE = "title"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment