主题 : 【代码】安卓程序sd卡里放raw文件代码实例 复制链接 | 浏览器收藏 | 打印
欢迎加入清源的android开发交流群:314230976,加群时请验证:arm,谢谢!
级别: 侠客
UID: 94332
精华: 0
发帖: 72
金钱: 370 两
威望: 74 点
贡献值: 0 点
综合积分: 144 分
注册时间: 2013-07-14
最后登录: 2013-09-25
楼主  发表于: 2013-07-24 16:18

 【代码】安卓程序sd卡里放raw文件代码实例

有时候我们需要把程序的raw文件放在sd卡中,其实有时候这样做可以释放资源,下面来看代码:

复制代码
  1. void copyAssets()
  2. {
  3. String[] files;
  4. try
  5. {
  6. files = this.getResources().getAssets().list("");
  7. }
  8. catch (IOException e1)
  9. {
  10. return;
  11. }
  12. if(!mWorkingPath.exists())
  13. {
  14. if(!mWorkingPath.mkdirs())
  15. {
  16. new AlertDialog.Builder(this)
  17. .setTitle(R.string.ERROR)
  18. .setMessage(R.string.FAILED_DIR_CREATE)
  19. .setPositiveButton(android.R.string.ok, new OnClickListener(){
  20. @Override
  21. public void onClick(DialogInterface dialog, int which)
  22. {
  23. dialog.dismiss();
  24. }
  25. })
  26. .create()
  27. .show();
  28. }
  29. }
  30. for(int i = 0; i < files.length; i++)
  31. {
  32. try
  33. {
  34. String fileName = files[i];
  35. if(fileName.compareTo("images") == 0
  36. fileName.compareTo("sounds") == 0
  37. fileName.compareTo("webkit") == 0)
  38. {
  39. continue;
  40. }
  41. File outFile = new File(mWorkingPath, fileName);
  42. if(outFile.exists()) continue;
  43. InputStream in = getAssets().open(fileName);
  44. OutputStream out = new FileOutputStream(outFile);
  45. // Transfer bytes from in to out
  46. byte[] buf = new byte[1024];
  47. int len;
  48. while ((len = in.read(buf)) > 0)
  49. {
  50. out.write(buf, 0, len);
  51. }
  52. in.close();
  53. out.close();
  54. }
  55. catch (FileNotFoundException e)
  56. {
  57. e.printStackTrace();
  58. }
  59. catch (IOException e)
  60. {
  61. e.printStackTrace();
  62. }
  63. }
欢迎加入android开发交流群,群号是:314230976