【发布时间】:2011-11-27 21:33:00
【问题描述】:
我有异步任务,必须将文件夹“文件”从资产写入 SD 卡上的文件夹。但没有任何效果。
final String sdDir = "/sdcard/izuchaika/";
new Thread(new Runnable() {
public void run() {
try {
InputStream in = getAssets().open("Files");
OutputStream out = new FileOutputStream(new File(sdDir));
try {
byte[] bucket = new byte[32 * 1024];
int bytesRead = 0;
while (bytesRead != -1) {
bytesRead = in.read(bucket);
out.write(bucket, 0, bytesRead);
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
if (in != null)
in.close();
if (out != null)
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
【问题讨论】:
-
一般提示:永远不要硬编码外部存储/sd卡的路径(就像你在代码的第1行中所做的那样)。请改用
Environment.getExternalStorageDirectory(),因为实际路径和存储类型因设备而异(例如,有些设备具有内置闪存而不是 SD 卡)
标签: java android file copy assets