From d13afd69c062d2dab2063ab90b21a2941f2099ac Mon Sep 17 00:00:00 2001 From: Scott Main Date: Thu, 12 Dec 2013 19:01:24 -0800 Subject: update storage doc for secondary external storage in KK and add some new sample code and remove documentation for APIs below level 8 and add information about permission changes in KK bug: 11907502 Change-Id: Id41c532fc2b05e59ab2cdf2b2a4ce368f7c2dba1 --- docs/html/guide/topics/data/data-storage.jd | 247 +++++++++++++++++----------- 1 file changed, 153 insertions(+), 94 deletions(-) (limited to 'docs/html') diff --git a/docs/html/guide/topics/data/data-storage.jd b/docs/html/guide/topics/data/data-storage.jd index 385c116..4b8a647 100644 --- a/docs/html/guide/topics/data/data-storage.jd +++ b/docs/html/guide/topics/data/data-storage.jd @@ -233,138 +233,197 @@ save files. This can be a removable storage media (such as an SD card) or an int (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.

-

It's possible that a device using a partition of the -internal storage for the external storage may also offer an SD card slot. In this case, -the SD card is not part of the external storage and your app cannot access it (the extra -storage is intended only for user-provided media that the system scans).

-

Caution: External storage can become unavailable if the user mounts the external storage on a computer or removes the media, and there's 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.

+

Getting access to external storage

+ +

In order to read or write files on the external storage, your app must acquire the +{@link android.Manifest.permission#READ_EXTERNAL_STORAGE} +or {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} system +permissions. For example:

+
+<manifest ...>
+    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
+    ...
+</manifest>
+
+ +

If you need to both read and write files, then you need to request only the +{@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission, because it +implicitly requires read access as well.

+ +

Note: Beginning with Android 4.4, these permissions are not +required if you're reading or writing only files that are private to your app. For more +information, see the section below about +saving files that are app-private.

+ +

Checking media availability

Before you do any work with the external storage, you should always call {@link android.os.Environment#getExternalStorageState()} to check whether the media is available. The media might be mounted to a computer, missing, read-only, or in some other state. For example, -here's how you can check the availability:

+here are a couple methods you can use to check the availability:

-boolean mExternalStorageAvailable = false;
-boolean mExternalStorageWriteable = false;
-String state = Environment.getExternalStorageState();
-
-if (Environment.MEDIA_MOUNTED.equals(state)) {
-    // We can read and write the media
-    mExternalStorageAvailable = mExternalStorageWriteable = true;
-} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
-    // We can only read the media
-    mExternalStorageAvailable = true;
-    mExternalStorageWriteable = false;
-} else {
-    // Something else is wrong. It may be one of many other states, but all we need
-    //  to know is we can neither read nor write
-    mExternalStorageAvailable = mExternalStorageWriteable = false;
+/* Checks if external storage is available for read and write */
+public boolean isExternalStorageWritable() {
+    String state = Environment.getExternalStorageState();
+    if (Environment.MEDIA_MOUNTED.equals(state)) {
+        return true;
+    }
+    return false;
+}
+
+/* Checks if external storage is available to at least read */
+public boolean isExternalStorageReadable() {
+    String state = Environment.getExternalStorageState();
+    if (Environment.MEDIA_MOUNTED.equals(state) ||
+        Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
+        return true;
+    }
+    return false;
 }
 
-

This example checks whether the external storage is available to read and write. The -{@link android.os.Environment#getExternalStorageState()} method returns other states that you +

The {@link android.os.Environment#getExternalStorageState()} method returns other states that you might want to check, such as whether the media is being shared (connected to a computer), is missing entirely, has been removed badly, etc. You can use these to notify the user with more information when your application needs to access the media.

-

Accessing files on external storage

- -

If you're using API Level 8 or greater, use {@link -android.content.Context#getExternalFilesDir(String) getExternalFilesDir()} to open a {@link -java.io.File} that represents the external storage directory where you should save your -files. This method takes a type parameter that specifies the type of subdirectory you -want, such as {@link android.os.Environment#DIRECTORY_MUSIC} and -{@link android.os.Environment#DIRECTORY_RINGTONES} (pass null to receive -the root of your application's file directory). This method will create the -appropriate directory if necessary. By specifying the type of directory, you -ensure that the Android's media scanner will properly categorize your files in the system (for -example, ringtones are identified as ringtones and not music). If the user uninstalls your -application, this directory and all its contents will be deleted.

- -

If you're using API Level 7 or lower, use {@link -android.os.Environment#getExternalStorageDirectory()}, to open a {@link -java.io.File} representing the root of the external storage. You should then write your data in the -following directory:

-
-/Android/data/<package_name>/files/
-
-

The {@code <package_name>} is your Java-style package name, such as "{@code -com.example.android.app}". If the user's device is running API Level 8 or greater and they -uninstall your application, this directory and all its contents will be deleted.

+

Saving files that can be shared with other apps

- -