Upload
cuthbert-kelley
View
214
Download
0
Embed Size (px)
Citation preview
Data Storage: Part 2(File System)
©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
©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.
©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
©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
©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
©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
©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
©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
©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
©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
©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
©SoftMoore Consulting
Example: Checking Media Availability(continued)
{ // We can neither read nor write. isExternalStorageAvailable = false; isExternalStorageWriteable = false; }
Slide 13
©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
©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
©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