您的位置:首页 > 移动开发 > Android开发

Android从Assets复制文件到本地

2018-01-15 13:50 344 查看
/***从asset复制文件到内存****/
private void copyByAssetsApk() {
new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("==============从asset复制文件到内存==============copyAssets============================.");
String newPath = FileUtils.DIR_apk;
File files = new File(newPath);
if (!files.exists()) {
files.mkdirs();
}
File file = new File(files, "Server.apk");
InputStream is = null;
try {
AssetManager manager = getAssets();
if (manager == null) return;
is = manager.open("Server.apk");
} catch (Exception e) {
e.printStackTrace();
}
if (is == null) return;
FileOutputStream fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int byteCount = 0;
while ((byteCount = is.read(buffer)) != -1) {// 循环从输入流读取
// buffer字节
fos.write(buffer, 0, byteCount);// 将读取的输入流写入到输出流
}
fos.flush();// 刷新缓冲区
is.close();
fos.close();
System.out.println("==============从asset复制文件到内存==============copyAssets  success============================.");
UpdateUtils.getIntance().installServerApk(file);
} catch (Exception e) {
System.out.println("==============从asset复制文件到内存==============copyAssets  er
4000
ror============================.");
e.printStackTrace();
}
}
}).start();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: