public class CopyFile extends Activity { private static String APP_NAME = "MyAssets"; private Button mButton; private LinearLayout mLayout; private AssetManager mAsset; private ProgressDialog mDialog; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRequestedOrientation(ActivityInfo .SCREEN_ORIENTATION_LANDSCAPE); mLayout = new LinearLayout(this); mButton = new Button(this); mLayout.setOrientation(LinearLayout.VERTICAL); mButton.setText("Copy File"); mButton.setLayoutParams(new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); mLayout.addView(mButton); setContentView(mLayout); mAsset = getAssets(); mDialog = new ProgressDialog(this); mButton.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { startCopy(); } }); } public void startCopy(){ showProgress(); Thread t = new Thread(new ProcessCopy()); t.start(); } public class ProcessCopy implements Runnable{ public void run(){ doCopy(); mHandler.sendEmptyMessage(MESSAGE_COMPLETE); } } public void onStart(){ super.onStart(); System.out.println("onStart......"); } public void doCopy(){ try { String[] arrFile = mAsset.list(""); if (arrFile != null) { for (int i = 0; i < arrFile.length; i++) { System.out.println(arrFile[i]); copyFile(arrFile[i]); } } } catch (IOException e) { e.printStackTrace(); } } /** * copy file from assets to sd */ public void copyFile(String path){ try { String[] list = mAsset.list(path); InputStream is = null; if (list != null && list.length != 0) { for (int i = 0; i < list.length; i++) { System.out.println(path + "/" + list[i]); copyFile(path + "/" + list[i]); } } else {//濡傛灉涓虹┖锛屽垯璁や负瀹冩槸涓�釜鏂囦欢 System.out.println("copy file: " + path); is = mAsset.open(path); saveFileToSD(is, path); } } catch (IOException e) { e.printStackTrace(); } } /** * save file to sd * @param is * @param path */ public void saveFileToSD(InputStream is, String path){ String sp = Environment.getExternalStorageDirectory() + "/" + APP_NAME + "/"; File f = new File(sp + path); String parentDir = f.getParent(); File parentFile = new File(parentDir); if (!parentFile.exists()) { parentFile.mkdirs(); System.out.println("create dir " +parentFile.getPath()); } if (!f.exists()) { try { f.createNewFile(); System.out.println("create new file " + f.getPath()); } catch (IOException e) { e.printStackTrace(); } } else { System.out.println("delete file " + f.getPath()); f.delete(); } try { DataInputStream dis = new DataInputStream(is); FileOutputStream fOut = new FileOutputStream(f); int n = 1024; byte[] buffer = new byte[n]; int num; while ((num = dis.available()) != 0) { dis.readFully(buffer, 0, num); fOut.write(buffer); } fOut.flush(); fOut.close(); dis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * show progress dialog */ public void showProgress(){ mDialog = ProgressDialog.show(this, "Title", "Copying, please wait..."); } public static final int MESSAGE_COMPLETE = 0; private final Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case MESSAGE_COMPLETE: mDialog.dismiss(); break; } } }; }