Reading and Writing SD card files in Android

Source: Internet
Author: User

1. Use the mksdcard command to create a 128 m sd card image named sdcard. IMG (m must be capitalized.
Mksdcard 128 M sdcard. img
2. Copy sdcard. IMG to D: \ projects2 and set the startup parameters in the run configuration dialog box in eclipse.
-Sdcard D: \ projects2 \ sdcard. img

In this way, the SD card is available.
3. add content to sdcard
ADB push E: \ Xunlei \ give.mp3/sdcard/give.mp3.
4. Read and Write files in the Application

Note: If you want to write the SD card in the application,

Android. Permission. write_external_storage must be added with the androi permission.

As follows:
<Uses-Permission Android: Name = "android. Permission. write_external_storage"> </uses-Permission>
Instance 1:
Button button6 = (button) findviewbyid (R. Id. button06 );

Onclicklistener listener6 = new onclicklistener (){

@ Override
Public void onclick (view v ){
Createsdfile ();
}
};
Button6.setonclicklistener (listener6 );
Button button7 = (button) findviewbyid (R. Id. button07 );
Onclicklistener listener7 = new onclicklistener (){
@ Override
Public void onclick (view v ){
Writesdfile (kfilename );
}
};
Button7.setonclicklistener (listener7 );

Button button8 = (button) findviewbyid (R. Id. button08 );
Onclicklistener listener8 = new onclicklistener (){
@ Override
Public void onclick (view v ){
Readsdfile (kfilename );
}
};
Button8.setonclicklistener (listener8 );



String kfilename = "/sdcard/uad2/test.txt ";

Void createsdfile ()

{

Createsdfile (kfilename );

}

Final Static string ksdcardroot = "sdcard ";

Void createsdfile (string filefullname)

{

If (filefullname. charat (0) = '/')
Filefullname = filefullname. substring (1, filefullname. Length ());
String name [] = filefullname. Split ("/");
If (name = NULL | Name. Length <2)

Return;

If (! Name [0]. Equals (ksdcardroot ))

{

Log. I (TAG, filefullname + "is not a file" + name [0]);

Return;

}

File sddir = new file ("/" + ksdcardroot );

If (! Sddir. exists ())

{

Log. I (TAG, "SD card is not exists ");

Return;

}

If (! Sddir. canwrite ())

{

Log. I (TAG, sddir. Length () + "SD card can not be wrote" + sddir. Canread ());

Return;

}

File dir = sddir;

For (INT I = 1; I <name. Length-1; I ++)

{

Dir = new file (dir. getabsolutepath () + "/" + name [I]);

If (dir. exists ())

Continue;

Boolean res = dir. mkdir ();

If (! Res)

{

Log. I (TAG, "Create dir:" + Res + "fail" + name [I]);

Return;

}

}

String filename = Name [name. Length-1]. Trim ();

If (filename. Length ()> 0)

{

File file = new file (dir. getabsolutepath () + "/" + name [name. Length-1]);

If (! File. exists ())

{

Try {

File. createnewfile ();

} Catch (ioexception E)

{

Log. E (TAG, "creat file erro:", e );

}

}

}

}

Void writesdfile (string filename)

{
File file = new file (filename );

If (! File. exists ())

Createsdfile (filename );

If (! File. canwrite ())

{

Log. I (TAG, "file:" + file. getabsolutefile () + "can not to be wrote ");

Return;

}

String content = "Hello:" + system. currenttimemillis ();

Fileoutputstream OS = NULL;

Try {

OS = new fileoutputstream (File );

OS. Write (content. getbytes ());

Log. I (TAG, "write:" + content );

}

Catch (ioexception E)

{

Log. E (TAG, "Write File", e );

}

Finally
{
If (OS! = NULL)
{
Try {
OS. Flush ();
OS. Close ();
} Catch (ioexception E)
{
Log. E (TAG, "Close file", e );
}
}
}

}

Void readsdfile (string filename)

{
String content;

Fileinputstream is = NULL;

File file = new file (filename );

If (! File. exists ())

{

Log. I (TAG, file. getabsolutepath () + "is not exists ");

Return;
}
If (! File. Canread ())

{

Log. I (TAG, file. getabsolutepath () + "can not be read ");

Return;
}
Try {

Is = new fileinputstream (File );

Byte buffer [] = new byte [is. Available ()];

Is. Read (buffer );

Content = new string (buffer );

Log. I (TAG, "Read SD file:" + content );

// Inputstreamreader inreader = new inputstreamreader (is );
}
Catch (ioexception E)

{

Log. E (TAG, "Write File", e );

}

Finally
{
If (is! = NULL)
{
Try {
Is. Close ();
} Catch (ioexception E)
{
Log. E (TAG, "Close file", e );
}
}
}

}

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.