解决Assert目录下无法拷贝超大文件到SD卡的问题

你。 提交于 2019-12-05 16:33:18

 Assert 目录文件拷贝时候, Android 有个规定就是文件大小不能操作1M, 不然会抛文件太大的错误. 解决办法如下. 将文件拷贝到类文件下:

 代码示意如下: 
 private static boolean copyFile(Context ctx, String filename, String des) {
  InputStream instream = null;
  try {
   if (filename.contains("TUIRes.ndt")) { //这个文件超过1M
    instream = XmlFile.class.getResourceAsStream("TUIRes.ndt"); 
   } else if (filename.contains("TUpdateRes.ndt")) {
    instream = XmlFile.class.getResourceAsStream("TUpdateRes.ndt");
   } else {
    instream = ctx.getAssets().open(filename);
   }

   copyFile(des, instream);
   return true;
  } catch (Exception e) {
   return false;
  }
 }

 private static void copyFile(String fileToPath, InputStream in)
   throws Exception {
  OutputStream out = null;
  try {

   out = new FileOutputStream(fileToPath);
   byte[] buffer = new byte[1024];
   while (true) {
    int ins = in.read(buffer);
    if (ins == -1) {
     break;
    }

    out.write(buffer, 0, ins);
   }
  } finally {
   if (in != null) {
    in.close();
   }
   if (out != null) {
    out.flush();
    out.close();
   }
  }
 }

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!