Applications can also use external buckets such as SD cards to access resources. This example describes how to use an external bucket to create and delete files.
In this example, the SD card is used to create files in three locations. The two files are private to the application. with the deletion of the application, the other is the Picuture directory on the shared SD card. The two private directories are private, but because they are on the SD card, other applications can also access them, but the directory name it creates is related to the application. In general, the uniqueness of the directory name can be guaranteed.
The Environment class can be used to query some environmental variables, such as whether the external memory (SD card) Is contained or not, and the Directory Name of the external memory. The following code gets the Picture directory name on the external storage:
[Java]
// CreateExternalStoragePublicPicture
File path = Environment. getExternalStoragePublicDirectory (
Environment. DIRECTORY_PICTURES );
File file = new File (path, "DemoPicture.jpg ");
// CreateExternalStoragePublicPicture
File path = Environment. getExternalStoragePublicDirectory (
Environment. DIRECTORY_PICTURES );
File file = new File (path, "DemoPicture.jpg ");
GetExternalFilesDir of the Context class can obtain the private directory of the application in the external storage. getExternalFilesDir can contain parameters or Null.
[Java]
// CreateExternalStoragePrivatePicture
File path = getExternalFilesDir (Environment. DIRECTORY_PICTURES );
File file = new File (path, "DemoPicture.jpg ");
...
// CreateExternalStoragePrivateFile
File file = new File (getExternalFilesDir (null), "DemoFile.jpg ");
// CreateExternalStoragePrivatePicture
File path = getExternalFilesDir (Environment. DIRECTORY_PICTURES );
File file = new File (path, "DemoPicture.jpg ");
...
// CreateExternalStoragePrivateFile
File file = new File (getExternalFilesDir (null), "DemoFile.jpg ");
The three methods correspond to the SD card location, as shown in:
In addition, this example also describes a custom View and the method for dynamically adding a View to a ViewGroup. In this example, we use two Layout resources:
R. layout. external_storage is the overall layout, and the root View is the LinearLayout with Id R. id. layout.
R. layout. external_storage_item is a custom control consisting of multiple views:
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "match_parent" android: layout_height = "wrap_content">
<TextView android: id = "@ + id/label" style = "? Android: attr/textAppearanceMediumInverse"
Android: layout_width = "match_parent" android: layout_height = "wrap_content"
Android: layout_weight = "0 ″
Android: padding = "4dip"
Android: singleLine = "true"
Android: color = "? Android: attr/textColorPrimaryInverse"
Android: background = "#888"/>
<TextView android: id = "@ + id/path" style = "? Android: attr/textAppearanceSmall"
Android: layout_width = "match_parent" android: layout_height = "wrap_content"
Android: layout_weight = "0 ″
Android: padding = "4dip"
Android: singleLine = "true"/>
<LinearLayout
Android: orientation = "horizontal"
Android: layout_width = "match_parent" android: layout_height = "wrap_content">
<View
Android: layout_width = "0dip"
Android: layout_height = "0dip"
Android: layout_weight = "1"/>
<Button android: id = "@ + id/create"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_weight = "1 ″
Android: text = "@ string/create"/>
<View
Android: layout_width = "0dip"
Android: layout_height = "0dip"
Android: layout_weight = "1"/>
<Button android: id = "@ + id/delete"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_weight = "1 ″
Android: text = "@ string/delete"/>
<View
Android: layout_width = "0dip"
Android: layout_height = "0dip"
Android: layout_weight = "1"/>
</LinearLayout>
</LinearLayout>
The View without ID defined in the above three bold words is a placeholder between the Create and Delete buttons.
The createStorageControls method uses R. layout. external_storage_item to redefine the text box display and Create and Delete callback functions for the template.
Then, the three custom components are dynamically added to Layout, which constitutes the final UI.
[Java]
MLayout. addView (mExternalStoragePublicPicture. mRoot );
MLayout. addView (mExternalStoragePrivatePicture. mRoot );
MLayout. addView (mExternalStoragePrivateFile. mRoot );
MLayout. addView (mExternalStoragePublicPicture. mRoot );
MLayout. addView (mExternalStoragePrivatePicture. mRoot );
MLayout. addView (mExternalStoragePrivateFile. mRoot );
Author: mapdigit