Read all files in assets (traverse each folder) and store them in sdcard.

Source: Internet
Author: User
Private void copyassets (string assetdir, string DIR ){
String [] files;
Try {
// Obtain multiple files in assets.
Files = This. getresources (). getassets (). List (assetdir );
} Catch (ioexception E1 ){
Return;
}
File mworkingpath = new file (DIR );
// If the file path does not exist
If (! Mworkingpath. exists ()){
// Create a folder
If (! Mworkingpath. mkdirs ()){
// Call when the Folder creation is unsuccessful
}
}

For (INT I = 0; I <files. length; I ++ ){
Try {
// Obtain the name of each file
String filename = files [I];
// Determine whether it is a folder or a file based on the path
If (! Filename. Contains (".")){
If (0 = assetdir. Length ()){
Copyassets (filename, Dir + filename + "/");
} Else {
Copyassets (assetdir + "/" + filename, Dir + "/"
+ Filename + "/");
}
Continue;
}
File OUTFILE = new file (mworkingpath, filename );
If (OUTFILE. exists ())
OUTFILE. Delete ();
Inputstream in = NULL;
If (0! = Assetdir. Length ())
In = getassets (). Open (assetdir + "/" + filename );
Else
In = getassets (). Open (filename );
Outputstream out = new fileoutputstream (OUTFILE );

// Transfer bytes from in to out
Byte [] Buf = new byte [1, 1024];
Int Len;
While (LEN = in. Read (BUF)> 0 ){
Out. Write (BUF, 0, Len );
}

In. Close ();
Out. Close ();
} Catch (filenotfoundexception e ){
E. printstacktrace ();
}

Catch (ioexception e ){
E. printstacktrace ();
}
}

}

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.