Use SDcard in Android to read files

Source: Internet
Author: User

We usually need to store large files such as audio and video on our mobile phones. We have learned how to store files (using File Operations) before. Considering the small storage space of mobile phones, at this time, we need to store the files in SDcard. Today we have learned how to store sdcard in android;

First, if you want to use sdcard for storage in the program, you must set the following permissions in the AndroidManifset. xml file:

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"/>

Then we will use several static methods under the Environment class when using SDcard for reading and writing.

1: getDataDirectory () obtains the data directory in Androi.

2: getDownloadCacheDirectory () obtains the downloaded cache directory.

3: getExternalStorageDirectory () the directory for obtaining external storage is generally SDcard

4: getExternalStorageState () obtains the current status of the external settings, which generally refers to SDcard,

In the android system, we usually use MEDIA_MOUNTED (SDcard exists and can be read and written) MEDIA_MOUNTED_READ_ONLY (SDcard exists and can only be read) of course there are other statuses that can be found in the document

5: getRootDirectory () obtains the Android Root path.

6: isExternalStorageEmulated () returns a Boolean value to determine whether the external settings are valid.

7: isExternalStorageRemovable () returns a Boolean value to determine whether the external settings can be removed.

[Note] We will use the red mark method above.

<Span style = "color: # ff0000;"> Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED) in the code, let's determine the status of the SDcard. </span> The following shows the Demo that implements the SDCard file read/write operation: package com. jiangqq. sdcard;

Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileOutputStream;

Import android. app. Activity;
Import android. content. Context;
Import android. OS. Bundle;
Import android. OS. Environment;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. Toast;

Public class SDcardActivity extends Activity {
Private Button bt1, bt2;
Private EditText et1, et2;

Private static final String FILENAME = "temp_file.txt ";

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Bt1 = (Button) this. findViewById (R. id. bt1 );
Bt2 = (Button) this. findViewById (R. id. bt2 );
Et1 = (EditText) this. findViewById (R. id. et1 );
Et2 = (EditText) this. findViewById (R. id. et2 );
Bt1.setOnClickListener (new MySetOnClickListener ());
Bt2.setOnClickListener (new MySetOnClickListener ());
}

Private class MySetOnClickListener implements OnClickListener {

@ Override
Public void onClick (View v ){
File file = new File (Environment. getExternalStorageDirectory (),
FILENAME );
Switch (v. getId ()){
Case R. id. bt1: // write using SDcard
If (Environment. getExternalStorageState (). equals (
Environment. MEDIA_MOUNTED )){

Try {
FileOutputStream fos = new FileOutputStream (file );
Fos. write (et1.getText (). toString (). getBytes ());
Fos. close ();
Toast. makeText (SDcardActivity. this, "file written successfully ",
Toast. LENGTH_LONG). show ();
} Catch (Exception e ){
Toast. makeText (SDcardActivity. this, "failed to write the file ",
Toast. LENGTH_SHORT). show ();
}
} Else {
// The SDcard does not exist or cannot be read or written.
Toast. makeText (SDcardActivity. this,
"SDcard does not exist or cannot perform read/write operations", Toast. LENGTH_SHORT). show ();
}
Break;

Case R. id. bt2: // read using SDcard
If (Environment. getExternalStorageState (). equals (
Environment. MEDIA_MOUNTED )){
Try {
FileInputStream inputStream = new FileInputStream (file );
Byte [] B = new byte [inputStream. available ()];
InputStream. read (B );
Et2.setText (new String (B ));
Toast. makeText (SDcardActivity. this, "file read successful ",
Toast. LENGTH_LONG). show ();
} Catch (Exception e ){
Toast. makeText (SDcardActivity. this, "Read failed ",
Toast. LENGTH_SHORT). show ();
}
} Else {
// The SDcard does not exist or cannot be read or written.
Toast. makeText (SDcardActivity. this,
"SDcard does not exist or cannot perform read/write operations", Toast. LENGTH_SHORT). show ();
}
Break;
}

}

}

}

The following results:


Demo download: http://files.cnblogs.com/greatverve/SDCardDemo.zip

Url: http://greatverve.cnblogs.com/archive/2012/01/13/android-SDcard.html

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.