SDcard to read the file

Source: Internet
Author: User

Usually we need to store large files on the phone like audio, video and so on, previously learned to use file for storage (using the file operation to store); Since the storage space of the phone itself is small, we need to store the file in SDcard. Today I also learned a bit on Android for sdcard storage usage;

First, if you want to use SDcard for storage in your program, we have to set the following permissions on the Androidmanifset.xml file:

The permissions to add access to SDcard in Androidmanifest.xml are as follows:

<!--Create and delete files in SDcard permissions--

<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

<!--write data to sdcard permissions--

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

Then we use SDcard to read and write, we use a few static methods under the Environment class.

1:getdatadirectory () Gets the data directory in the Androi

2:getdownloadcachedirectory () get to the downloaded cache directory

3:getexternalstoragedirectory () Gets the directory to the external storage generally refers to SDcard

4:getexternalstoragestate () Gets the current state of the external setting generally refers to SDcard,

For the state of the external settings in the Android system, we are more commonly used media_mounted (SDcard exists and can read and write) media_mounted_read_only (SDcard exist, can only be read operation) Of course there are other states that can be found in the documentation

5:getrootdirectory () get to Android Root path

6:isexternalstorageemulated () returns a Boolean value to determine if the external setting is valid

7:isexternalstorageremovable () returns a Boolean value that determines whether external settings can be removed

"Note" Above the method of red marking, we will often

[Java]View Plaincopy
    1. <span style="color: #ff0000;" >environment.getexternalstoragestate (). Equals (environment.media_mounted) code in this sentence: We judge the state of SDcard, </span >

The following is a demo that implements SDcard for file read and write operations:

[Java]View Plaincopy
  1. Package Com.jiangqq.sdcard;
  2. Import Java.io.File;
  3. Import Java.io.FileInputStream;
  4. Import Java.io.FileOutputStream;
  5. Import android.app.Activity;
  6. Import Android.content.Context;
  7. Import Android.os.Bundle;
  8. Import android.os.Environment;
  9. Import Android.view.View;
  10. Import Android.view.View.OnClickListener;
  11. Import Android.widget.Button;
  12. Import Android.widget.EditText;
  13. Import Android.widget.Toast;
  14. Public class Sdcardactivity extends Activity {
  15. private Button bt1, BT2;
  16. private EditText et1, Et2;
  17. private static final String FILENAME = "Temp_file.txt";
  18. @Override
  19. public void OnCreate (Bundle savedinstancestate) {
  20. super.oncreate (savedinstancestate);
  21. Setcontentview (R.layout.main);
  22. BT1 = (Button) This.findviewbyid (R.ID.BT1);
  23. BT2 = (Button) This.findviewbyid (R.ID.BT2);
  24. Et1 = (EditText) This.findviewbyid (R.ID.ET1);
  25. Et2 = (EditText) This.findviewbyid (R.id.et2);
  26. Bt1.setonclicklistener (new Mysetonclicklistener ());
  27. Bt2.setonclicklistener (new Mysetonclicklistener ());
  28. }
  29. private class Mysetonclicklistener implements Onclicklistener {
  30. @Override
  31. public void OnClick (View v) {
  32. File File = new file (Environment.getexternalstoragedirectory (),
  33. FILENAME);
  34. switch (V.getid ()) {
  35. case R.ID.BT1://Use SDcard write operation
  36. if (Environment.getexternalstoragestate (). Equals (
  37. environment.media_mounted)) {
  38. try {
  39. FileOutputStream fos = new FileOutputStream (file);
  40. Fos.write (Et1.gettext (). toString (). GetBytes ());
  41. Fos.close ();
  42. Toast.maketext (sdcardactivity. This, "write file succeeded",
  43. Toast.length_long). Show ();
  44. } catch (Exception e) {
  45. Toast.maketext (sdcardactivity. This, "write file Failed",
  46. Toast.length_short). Show ();
  47. }
  48. } Else {
  49. //At this time SDcard does not exist or can not be read and write operations
  50. Toast.maketext (sdcardactivity. This,
  51. "At this time SDcard does not exist or can not read and write operations", Toast.length_short). Show ();
  52. }
  53. Break ;
  54. Case R.ID.BT2://using SDcard read operation
  55. if (Environment.getexternalstoragestate (). Equals (
  56. environment.media_mounted)) {
  57. try {
  58. FileInputStream InputStream = new FileInputStream (file);
  59. byte[] B = new byte[inputstream.available ()];
  60. Inputstream.read (b);
  61. Et2.settext (new String (b));
  62. Toast.maketext (sdcardactivity. This, "read file succeeded",
  63. Toast.length_long). Show ();
  64. } catch (Exception e) {
  65. Toast.maketext (sdcardactivity. This, "read failed",
  66. Toast.length_short). Show ();
  67. }
  68. } Else {
  69. //At this time SDcard does not exist or can not be read and write operations
  70. Toast.maketext (sdcardactivity. This,
  71. "At this time SDcard does not exist or can not read and write operations", Toast.length_short). Show ();
  72. }
  73. Break ;
  74. }
  75. }
  76. }
  77. }

The following effect:

Demo Download Link: http://download.csdn.net/detail/jiangqq781931404/4017475

SDcard to read the file

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.