读书人

将assets目录下的一些图片拷贝到sdcar

发布时间: 2012-02-06 15:52:45 作者: rapoo

将assets目录下的一些图片拷贝到sdcard中
使用

Java code
Runtime.getRuntime().exec("cp file:///android_asset/* /mnt/sdcard/images/");


用上面的方法提示说权限不足

我尝试了下用输入输出流先把assets的文件读入到输入流,再用输出流写到sdcard中,这样是可以的,除此之外还有别的方法吗?如果文件多的话,这样作效率很低的

[解决办法]
好像不可以,用IO流读写才行~~~
[解决办法]
private void copyGloab2Databases() {
File file = new File("/data/data/" + getPackageName() + "/databases/",
Utils.GLOBALDB+".db");
file.getParentFile().mkdirs();
if(file.exists())
return;

InputStream in;
OutputStream out;
try {
in = getAssets().open(Utils.GLOBALDB+".db");
out = new FileOutputStream(file);

byte[] buff = new byte[1024];
int len;
while ((len = in.read(buff)) > 0) {
out.write(buff, 0, len);
}

out.close();
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
[解决办法]
file:///android_asset/*是用于java代码中调用assets目录下的所有文件
但是Runtime.getRuntime().exec("cp file:///android_asset/* /mnt/sdcard/images/");
这个命令是linux命令
你可以试试Runtime.getRuntime().exec("cp assets/* /mnt/sdcard/images/");
[解决办法]
探讨

应该不可以的,看样子只能用读IO了

读书人网 >Android

热点推荐