Data storage and access 3_scard

Source: Internet
Author: User

Use the openFileOutput () method of Activity to save files. The files are stored in the mobile phone space. Generally, the mobile phone storage space is not very large, and it is okay to store some small files, it is not feasible to store large files such as videos. For large files like videos, we can store them in SDCard. What does SDCard do? You can think of it as a mobile hard disk or a USB flash disk.


To use SDCard in the simulator, You need to first create an SDCard (of course, not a real SDCard, but an image file ). You can create SDCard along with the simulator created in Eclipse, or use the doscommand to create it, as shown below:
Enter the tools directory in the android SDK installation path in the Dos window, and enter the following command to create an SDCard with a capacity of 2 GB. The file suffix can be obtained at will. img is recommended:
Mksdcard 2048 m d: \ AndroidTool \ sdcard. img

To access SDCard in a program, you need to apply for the permission to access SDCard.
The permission to access SDCard in AndroidManifest. xml is as follows:
<! -- Create and delete file permissions in SDCard -->
<Uses-permission android: name = "android. permission. MOUNT_UNMOUNT_FILESYSTEMS"/>
<! -- Write data to SDCard -->

<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>

To store files in SDCard, the program must first determine whether the mobile phone is equipped with SDCard and can read and write data.

Note: The permission to access SDCard must be added to AndroidManifest. xml.
If (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED )){
File sdCardDir = Environment. getExternalStorageDirectory (); // get the SDCard directory
File saveFile = new File (sdCardDir, “itcast.txt ");
FileOutputStream outStream = new FileOutputStream (saveFile );
OutStream. write ("Zhao yazhi blog". getBytes ());
OutStream. close ();
}
The Environment. getExternalStorageState () method is used to obtain the SDCard status. If the mobile phone has an SDCard and can be read and written, the returned state is Environment. MEDIA_MOUNTED.
The Environment. getExternalStorageDirectory () method is used to obtain the SDCard directory. To obtain the SDCard directory, you can also write:
File sdCardDir = new File ("/sdcard"); // get the SDCard directory
File saveFile = new File (sdCardDir, "itcast.txt ");
// The code above can be combined into a sentence: File saveFile = new File ("/sdcard/itcast.txt ");
FileOutputStream outStream = new FileOutputStream (saveFile );
OutStream. write ("Zhao yazhi test". getBytes ());
OutStream. close ();

 

 

 


Instance login


AndroidManifest. xml
[Html] view plaincopyprint?
<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. example. lession03_login_sd"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
 
<Uses-sdk
Android: minSdkVersion = "8"
Android: targetSdkVersion = "17"/>
<Uses-permission android: name = "android. permission. MOUNT_UNMOUNT_FILESYSTEMS"/>
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>
 
<Application
Android: allowBackup = "true"
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name"
Android: theme = "@ style/AppTheme">
<Activity
Android: name = "com. example. lession03_login_sd.LoginActivity"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
 
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
</Application>
 
</Manifest>

<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. example. lession03_login_sd"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">

<Uses-sdk
Android: minSdkVersion = "8"
Android: targetSdkVersion = "17"/>
<Uses-permission android: name = "android. permission. MOUNT_UNMOUNT_FILESYSTEMS"/>
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>

<Application
Android: allowBackup = "true"
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name"
Android: theme = "@ style/AppTheme">
<Activity
Android: name = "com. example. lession03_login_sd.LoginActivity"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>

<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
</Application>

</Manifest>

 

Layout File
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">
 
<LinearLayout
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content">
 
<TextView
Android: id = "@ + id/view_name"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/login_name"/>
 
<EditText
Android: id = "@ + id/edit_name"
Android: layout_width = "0dp"
Android: layout_height = "wrap_content"
Android: layout_weight = "1"
Android: EMS = "10"
Android: inputType = "textPersonName">
 
<RequestFocus/>
</EditText>
</LinearLayout>
 
<LinearLayout
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content">
 
<TextView
Android: id = "@ + id/view_pass"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/login_pass"/>
 
<EditText
Android: id = "@ + id/edit_pass"
Android: layout_width = "0dp"
Android: layout_height = "wrap_content"
Android: layout_weight = "1"
Android: EMS = "10"
Android: inputType = "textPersonName">
 
<RequestFocus/>
</EditText>
</LinearLayout>
 
<LinearLayout
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content">
 
<RadioGroup
Android: id = "@ + id/radioGroup1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: orientation = "horizontal"
Tools: ignore = "UselessParent">
 
<RadioButton
Android: id = "@ + id/radio_rom"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: checked = "true"
Android: text = "@ string/login_rom"/>
 
<RadioButton
Android: id = "@ + id/radio_sp"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/login_sp"/>
 
<RadioButton
Android: id = "@ + id/radio_sd"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/login_sd"/>
</RadioGroup>
 
</LinearLayout>
<LinearLayout
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content">
 
<Button
Android: id = "@ + id/button_login"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/login_login"/>
 
<CheckBox
Android: id = "@ + id/check_remember"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_marginLeft = "100dp"
Android: text = "@ string/login_remember"/>
</LinearLayout>
 
 
 
</LinearLayout>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.