我遇到以下代码的问题,它会将文件复制到内部sd。问题是它只会在我第二次运行应用程序时才能运行。我需要它在第一次运行时复制我不知道我做错了什么可以请任何人帮忙。
private void CopyPak()
{
try {
InputStream in = getResources().openRawResource(R.raw.bor);
File outFolder = new File(Environment.getExternalStorageDirectory() + "/OpenBOR/Paks");
File outFile = new File(outFolder, "bor.pak");
if(!outFolder.exists()){
outFolder.mkdir();
}
FileOutputStream out = new FileOutputStream(outFile);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (IOException e) {
Log.e("tag", "Failed to copy asset file: ", e);
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
}
以下是必需的导入。
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
答案 0 :(得分:0)
发现错误我必须将mkdir
替换为mkdirs
,因为我创建了多个文件夹。