Android program functions copy files under the Assets folder to your phone's SD card (including subfolders)

Source: Internet
Author: User

A recent function is to copy all files (including sub-files) under the Asset folder to the specified directory. The method used is to use Assetmanager. But here's the problem is to say that the subfolders and sub-files are to be copied out. To the internet Google, also to Baidu search under, found a lot of similar problems. But there seems to be a problem. Obviously can only be done to copy the files in the asset direct directory, but the sub-folder is not copied, and, in the folder, will throw an exception. Helpless himself had to write a. As follows:

private void Copyassets (String assetdir,string dir) {
string[] files;
Try
{
Files = This.getresources (). Getassets (). List (Assetdir);
}
catch (IOException E1)
{
Return
}
File Mworkingpath = new file (dir);
If this directory is does not exists, make one.
if (!mworkingpath.exists ())
{
if (!mworkingpath.mkdirs ())
{

}
}

for (int i = 0; i < files.length; i++)
{
Try
{
String fileName = files[i];
We make sure the file name not contains '. ' To is a folder.
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
byte[] buf = new byte[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 ();
}
}

The main use of recursion here, the other is nothing. There is a problem here: How to determine whether the path is a folder or a file, I am currently using the method is to determine whether there is a suffix, this may be problematic.

(ext: http://www.cnblogs.com/ctou45/archive/2011/06/01/2065460.html)

Android program functions copy files under the Assets folder to your phone's SD card (including subfolders)

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.