Android reads and writes SD cards

Source: Internet
Author: User

The read and write operations on folders and files in Android are the same as those in Java. However, you must specify the user's permissions to run the commands on Android. Modify the androidmanifest. xml file and add the following Configuration:

     <! --  Permission to write data to sdcard  --> 
< Uses-Permission Android: Name = "Android. Permission. write_external_storage" > </ Uses-Permission >
<! -- Permission to create/delete files in sdcard -->
< Uses-Permission Android: Name = "Android. Permission. mount_unmount_filesystems" > </ Uses-Permission >

After this configuration, you can operate on a real machine like Java.CompositionAnd Folders:

 Public   Class Sdcardhelper {

Public Static Void Deletedirectory (string path ){
File dir = New File (PATH );
If (Dir. exists ()){
Dir. Delete ();
}
}

Public Static Boolean Createdirectory (string path ){
File dir = New File (PATH );
If (! Dir. exists ()){
Dir. mkdirs ();
}
If (Dir. exists ()){
Return True ;
} Else {
Return False ;
}
}

Public Static String getdirectory (string subdir ){
String Path = Android. OS. environment. getexternalstoragedirectory ()
. Getabsolutepath () + file. Separator;
Return Path + subdir;
}

Public Static Boolean Hassdcard (){
String status = environment. getexternalstoragestate ();
If (Status. Equals (environment. media_mounted )){
Return True ;
} Else {
Return False ;
}
}

}

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.