我正在尝试将存储在应用的资源文件夹中的一些预加载内容复制到sdk卡。问题是我似乎无法抓住我要复制的目录的文件路径。我想循环遍历项目中我的assets文件夹中存储的preloadedcontent文件夹,然后将其中的每个文件夹复制到sdk卡。我可以循环遍历预加载的内容文件名,但是当我尝试复制目录时,会得到一个filenotfound异常:
- Assets/
-- preloadedcontent/
--- 112/
--- 113/
--- 114/
private void copyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("preloadedcontent");
} catch (IOException e) {
Log.e("tag", "Failed to get asset file list.", e);
}
for(String filename : files) {
Log.d("file: ",filename);
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open("preloadedcontent/" + filename + "/");
File outFile = new File(_DirectoryName, filename);
out = new FileOutputStream(outFile);
//copyFile(in, out);
File f = stream2file(in,filename);
copyDirectory(f,outFile);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
}
}
}
错误出现在此行assetManager.open(“preloadedcontent /”+ filename +“/”);
答案 0 :(得分:0)
那是因为"preloadedcontent/" + filename + "/"
不是有效的文件名(由于斜杠)。
AssetManager#open()
需要打开单个文件的名称。
答案 1 :(得分:0)
if(id.equals(" AABA / ABO")){
AssetManager assetManager = activity.getAssets();
String[] files;
try {
files = assetManager.list("aaba");
List<String> it = Arrays.asList(files);
for (String string : it) {
InputStream ims = assetManager.open("aaba/" + string);
// create drawable from stream
Drawable d = Drawable.createFromStream(ims, null);
drawable.add(d);
}
} catch (IOException e) {
e.printStackTrace();
}
}