我有一个问题。
我想将资产forder(Path:Assets / Manual)中的文件复制到另一个目录(Environment.DIRECTORY_DOWNLOADS)。
所以我写下面的源代码。
private void copyAssetManual(){
AssetManager assetManager = getAssets();
String[] files = null;
try{
files = assetManager.list("Manual");
Log.d(TAG, "Files String Array Length: " + files.length);
}catch(IOException e){
Log.d(TAG, e.getMessage());
}
for(String filename : files){
Log.d(TAG, "copyAssetManual: " + filename);
InputStream in = null;
OutputStream out = null;
try{
in = assetManager.open("Manual/" + filename);
out = new FileOutputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "/" + filename);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
}catch(Exception e){
Log.d(TAG, e.getMessage());
}
//Auto Execute Copied File
File userManual = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), filename);
String extension = MimeTypeMap.getFileExtensionFromUrl(filename);
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.toLowerCase());
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(userManual), mimeType);
startActivity(intent);
}
}
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);
}
}
确实有效!
但是,当我在资源文件夹(路径:资产/手动)中替换文件(相同文件而不是不同文件名)时,此源代码记录了以前的所有文件名列表。
离)
资产文件夹有AAA.pdf
此源代码打印“copyAssetManual:AAA.pdf”
之后,我删除了AAA.pdf并将BBB.pdf复制到资产文件夹(路径:资产/手册)
然后这个源代码打印两次,如下所示。
“copyAssetManual:AAA.pdf”
“copyAssetManual:BBB.pdf”
现在资产文件夹中没有AAA.pdf!
我该如何处理这个问题?
我不需要以前的文件名列表(AAA.pdf)....
请帮帮我..
答案 0 :(得分:1)
您无法将文件复制到assets文件夹中。 在您从assets文件夹复制到外部SD卡文件系统的第一个代码段中
答案 1 :(得分:0)
我终于对待了它!
只需单击“代码清理”菜单按钮即可。