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)