External file storage for android-data storage (sdcard)

Source: Internet
Author: User

I. Basic outline

1. Description:

1> the data files used to run the application can be saved to the SD card

2> file Type: arbitrary

3> Data Save path:

Path 1:/storage/sdcard/android/data/packagename/files Other apps can access, remove when applying uninstall

Path 2:/storage/sdcard/xxx/(indicates that the file you created--xxx) can be accessed by other apps and will not be deleted when the app is uninstalled

2. Related API

Environment Operation SD Card tool class:

---get SD card status: Environment.getexternalstoragestate ()

SD card can read and write Mount status value: environment.media_mounted

---get SD card path: Environment.getexternalstoragedirectory ()

Context.getexternalfilesdir ():

---get/mnt/sdcard/android/data/package_name/files/xxx.txt

    

Permissions to operate the SD card:

---android.permission.WRITE_EXTERNAL_STORAGE is write-only, but it can be read after it is added.

Ii. Development Steps (Path 1)

1. Write Data

1> determine the status of the SD card if the status of the Mount continues

2> get input file name/content

3> gets the outputstream of the specified file:

. Get the files path under the SD card

. Make a full path

. Create FileOutputStream

4> Writing data

if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {

String filename= "XRK";
String filecontent= "I am not a sunflower";
String filespath=getexternalfilesdir (null). GetAbsolutePath ();
String filepath=filespath+ "/" +filename;
FileOutputStream fos=new FileOutputStream (FilePath);
Fos.write (Filecontent.getbytes ("Utf-8"));
Fos.close ();
Toast.maketext (Mainactivity.this, "saved successfully", Toast.length_short). Show ();
}else{
Toast.maketext (Mainactivity.this, "Save Yes than", Toast.length_short). Show ();
}

2. Read the data:

if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {

String filename= "XRK";
String filespath=getexternalfilesdir (null). GetAbsolutePath ();
System.out.println ("Iueiudshcs" +filespath);
String filepath=filespath+ "/" +filename;
FileInputStream fis=new FileInputStream (FilePath);
Bytearrayoutputstream baos=new Bytearrayoutputstream ();
Byte[] Buffer=new byte[1024];
int len=-1;
while ((Len=fis.read (buffer))!=-1) {
Baos.write (buffer, 0, Len);
}
String content=baos.tostring ();


Toast.maketext (Mainactivity.this, "read succeeded" +content, Toast.length_short). Show ();
}else{
Toast.maketext (Mainactivity.this, "read failed", Toast.length_short). Show ();
}

 

Iii. Development Steps (Path 2)

1. Write Data

if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {

String filename= "Cheriong. txt";
String filecontent= "I am not a sunflower";
Gets the output stream of the specified file
String sdpath=environment.getexternalstoragedirectory (). GetAbsolutePath ();
File File=new file (sdpath+ "/atguigu");
if (!file.exists ()) {
File.mkdirs ();//Create Folder
}
String filepath=sdpath+ "/atguigu/" +filename;
String filespath=getexternalfilesdir (null). GetAbsolutePath ();
System.out.println ("Iueiudshcs" +filespath);
FileOutputStream fos=new FileOutputStream (FilePath);

Fos.write (Filecontent.getbytes ("Utf-8"));
Fos.close ();
Toast.maketext (Mainactivity.this, "saved successfully", Toast.length_short). Show ();
}else{
Toast.maketext (Mainactivity.this, "Save Yes than", Toast.length_short). Show ();
}

2. Read the data:

if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {

String filename= "Cheriong. txt";
String sdpath=environment.getexternalstoragedirectory (). GetAbsolutePath ();
String filepath=sdpath+ "/atguigu/" +filename;
FileInputStream fis=new FileInputStream (FilePath);
Bytearrayoutputstream baos=new Bytearrayoutputstream ();
Byte[] Buffer=new byte[1024];
int len=-1;
while ((Len=fis.read (buffer))!=-1) {
Baos.write (buffer, 0, Len);
}
String content=baos.tostring ();
Fis.close ();

Toast.maketext (Mainactivity.this, "read succeeded" +content, Toast.length_short). Show ();
}else{
Toast.maketext (Mainactivity.this, "read failed", Toast.length_short). Show ();
}

  

External file storage for android-data storage (sdcard)

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.