我有个问题。
我希望将文件从assets (路径:Asset/手工)复制到另一个目录(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).
帮帮我求你..。
发布于 2017-04-28 14:52:58
我终于治好了!
只需单击“代码清理”菜单按钮。
发布于 2017-04-28 07:56:45
无法将文件复制到“资产”文件夹中。在第一个代码片段中,您将从资产文件夹复制到外部sd卡文件系统中。
https://stackoverflow.com/questions/43674045
复制相似问题