16
Data Storage: Part 2 (File System)

Data Storage: Part 2 (File System). Internal Storage versus External Storage Internal storage − for private data –By default, files saved to the internal

Embed Size (px)

Citation preview

Page 1: Data Storage: Part 2 (File System). Internal Storage versus External Storage Internal storage − for private data –By default, files saved to the internal

Data Storage: Part 2(File System)

Page 2: Data Storage: Part 2 (File System). Internal Storage versus External Storage Internal storage − for private data –By default, files saved to the internal

©SoftMoore Consulting

Internal Storage versus External Storage

• Internal storage − for private data– By default, files saved to the internal storage are private to your

application and other applications cannot access them.– When the user uninstalls an application, files and directories

created by that application in internal storage are removed.

• External storage − for public shared data– Every Android-compatible device supports a shared “external

storage” that can be used to save files.– External storage can be

• a removable storage media (such as an SD card)• an internal (non-removable) storage.

– Files saved to the external storage are world-readable and can be modified by the user when they enable USB mass storage to transfer files on a computer.

Slide 2

Page 3: Data Storage: Part 2 (File System). Internal Storage versus External Storage Internal storage − for private data –By default, files saved to the internal

©SoftMoore Consulting

Using Java I/O for Data Storage

• The standard Java I/O capabilities provided in package java.io can be used to read and write data files on an Android device’s internal and external storage.

• Android provides several helper methods inherited from android.content.Context that can be used access files on the device.

• Application files are saved in the folder/data/data/<package name>/files

• DDMS can be used to explore a device’s file system and to transfer files between a computer and an Android device.

Slide 3

Access to the internal storage of a “real” device is restricted.

Page 4: Data Storage: Part 2 (File System). Internal Storage versus External Storage Internal storage − for private data –By default, files saved to the internal

©SoftMoore Consulting

Helper Methods Inherited Fromandroid.content.Context

• FileInputStream openFileInput(String name)– Open a private file for reading.

• FileOutputStream openFileOutput(String name, int mode)– Open a private file for writing or appending.

• File getFilesDir()– Get the absolute path to the file system directory where your

internal files are saved (i.e., returns a File object for/data/data/<package name>/files/).

• File getDir(String name, int mode)– Create (or open an existing) directory within your internal storage

space. Slide 4

Page 5: Data Storage: Part 2 (File System). Internal Storage versus External Storage Internal storage − for private data –By default, files saved to the internal

©SoftMoore Consulting

Helper Methods Inherited Fromandroid.content.Context (continued)

• boolean deleteFile(String name)– Delete the given private file associated with this application.– Returns true if the file was successfully deleted; otherwise

returns false.

• String[] fileList()– Returns an array of strings naming the private files associated

with this application.

Slide 5

Page 6: Data Storage: Part 2 (File System). Internal Storage versus External Storage Internal storage − for private data –By default, files saved to the internal

©SoftMoore Consulting

File Modes

Selected file modes from android.content.Context

• MODE_APPEND– If the file already exists, then write data to the end of the existing

file instead of erasing it.

• MODE_PRIVATE– Created file can only be accessed by the calling application

(default mode).

• MODE_WORLD_READABLE (deprecated in API level 17)– Allow all other applications read access to the created file.

• MODE_WORLD_WRITEABLE (deprecated in API level 17)– Allow all other applications write access to the created file.

Slide 6

Page 7: Data Storage: Part 2 (File System). Internal Storage versus External Storage Internal storage − for private data –By default, files saved to the internal

©SoftMoore Consulting

Example: Reading from a Text File

BufferedReader br = null; try { FileInputStream fis = openFileInput(FILE_NAME); br = new BufferedReader(new InputStreamReader(fis)); StringBuilder builder = new StringBuilder(200); String line = null;

while((line = br.readLine()) != null) builder.append(line + '\n'); EditText editText = (EditText) findViewById(R.id.editText); editText.setText(builder.toString()); }

Slide 7

Page 8: Data Storage: Part 2 (File System). Internal Storage versus External Storage Internal storage − for private data –By default, files saved to the internal

©SoftMoore Consulting

Example: Reading from a Text File(continued)

catch (FileNotFoundException ex) { String errorMsg = FILE_NAME + " not found"; Log.e(LOG_TAG, errorMsg, ex); Toast toast = Toast.makeText(FileStorage.this, errorMsg, Toast.LENGTH_SHORT); toast.show(); }catch (IOException ex) { ... // log and report error message }finally { ... // if br != null, try to call br.close() // log and report error message }

Slide 8

Page 9: Data Storage: Part 2 (File System). Internal Storage versus External Storage Internal storage − for private data –By default, files saved to the internal

©SoftMoore Consulting

Example: Writing to a Text File

BufferedWriter bw = null; try { FileOutputStream fos = openFileOutput(FILE_NAME, MODE_PRIVATE); bw = new BufferedWriter(new OutputStreamWriter(fos)); EditText editText = (EditText) findViewById(R.id.editText); String text = editText.getText().toString(); bw.write(text, 0, text.length()); }

Slide 9

Page 10: Data Storage: Part 2 (File System). Internal Storage versus External Storage Internal storage − for private data –By default, files saved to the internal

©SoftMoore Consulting

Example: Writing to a Text File(continued)

catch (FileNotFoundException ex) { String errorMsg = FILE_NAME + " not found"; Log.e(LOG_TAG, errorMsg, ex); Toast toast = Toast.makeText(FileStorage.this, errorMsg, Toast.LENGTH_SHORT); toast.show(); }catch (IOException ex) { ... // log and report error message }finally { ... // if bw != null, try to call bw.close() // log and report error message }

Slide 10

Page 11: Data Storage: Part 2 (File System). Internal Storage versus External Storage Internal storage − for private data –By default, files saved to the internal

©SoftMoore Consulting

Checking Media Availabilityfor External Storage

• There is no security enforced upon files you save to the external storage. All applications can read and write files placed on the external storage and the user can remove them.

• Call getExternalStorageState() before using external storage to check whether the media is available.

• The media could be mounted to a computer, missing, read-only, or in some other state.

Slide 11

Page 12: Data Storage: Part 2 (File System). Internal Storage versus External Storage Internal storage − for private data –By default, files saved to the internal

©SoftMoore Consulting

Example: Checking Media Availability

boolean isExternalStorageAvailable = false;boolean isExternalStorageWriteable = false;String state = Environment.getExternalStorageState();

if (state.equals(Environment.MEDIA_MOUNTED)) {    // We can both read and write.    isExternalStorageAvailable = true; isExternalStorageWriteable = true; }else if (state.equals(Environment.MEDIA_MOUNTED_READ_ONLY) {    // We can only read.    isExternalStorageAvailable = true;    isExternalStorageWriteable = false; }else // (continued on next slide)

Slide 12

Page 13: Data Storage: Part 2 (File System). Internal Storage versus External Storage Internal storage − for private data –By default, files saved to the internal

©SoftMoore Consulting

Example: Checking Media Availability(continued)

{    // We can neither read nor write.    isExternalStorageAvailable = false; isExternalStorageWriteable = false; }

Slide 13

Page 14: Data Storage: Part 2 (File System). Internal Storage versus External Storage Internal storage − for private data –By default, files saved to the internal

©SoftMoore Consulting

Public Directories on External Storage

Files saved in one of the public directories on external storage are not deleted when an application is uninstalled. Examples include the following

• Music/ - Media scanner classifies all media found here as user music.

• Ringtones/ - Media scanner classifies all media found here as a ringtone.

• Alarms/ - Media scanner classifies all media found here as an alarm sound.

• Pictures/ - All photos (excluding those taken with the camera).

• Movies/ - All movies (excluding those taken with the camcorder).

Slide 14

Page 15: Data Storage: Part 2 (File System). Internal Storage versus External Storage Internal storage − for private data –By default, files saved to the internal

©SoftMoore Consulting

Accessing Files on External Storage

• For API Level 8 or greater– Use getExternalFilesDir() to open a File that represents

the external storage directory where you should save your files.– Parameter specifies the type of subdirectory.

• DIRECTORY_MUSIC• DIRECTORY_RINGTONES• ...• null to get the root of your application’s file directory

– Method will create the appropriate directory if necessary.

• API Level 7 or lower– Use getExternalStorageDirectory() to open a File

representing the root of the external storage.– Write your data in the following directory:/Android/data/<package_name>/files/

Slide 15

Page 16: Data Storage: Part 2 (File System). Internal Storage versus External Storage Internal storage − for private data –By default, files saved to the internal

©SoftMoore Consulting

Relevant Links

• Data Storagehttp://developer.android.com/guide/topics/data/data-storage.html

• Class Contexthttp://developer.android.com/reference/android/content/Context.html

• The Java Tutorials: Basic I/Ohttp://download.oracle.com/javase/tutorial/essential/io/

Slide 16