【发布时间】:2017-09-26 05:00:37
【问题描述】:
我有问题。
我想将文件从 assets forder(路径: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 复制到 Assets 文件夹(路径:Assets/Manual)
然后这个源代码打印两次,如下所示。
"copyAssetManual: AAA.pdf"
"copyAssetManual: BBB.pdf"
现在资产文件夹中没有 AAA.pdf!
我该如何处理这个问题?
我不需要以前的文件名列表(AAA.pdf)....
请帮帮我..
【问题讨论】: