我有一个问题。
我想将文件从 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);
}
}
它确实有效!
但是,当我替换 Assets 文件夹(路径: Assets /手册)中的文件(相同文件而不是不同文件名)时,此源代码记录了所有以前的文件名列表。
例)
Assets Folder has AAA.pdf
This source code printed "copyAssetManual: AAA.pdf"
After that, I deleted AAA.pdf and copy BBB.pdf to Assets Folder(Path: Assets/Manual)
then this source code printed twice like below.
"copyAssetManual: AAA.pdf"
"copyAssetManual: BBB.pdf"
Assets 文件夹中现在没有 AAA.pdf!
我该如何处理这个问题?
我不需要以前的文件名列表(AAA.pdf)....
请帮帮我..
最佳答案
您不能将文件复制到 Assets 文件夹中。 在您从 Assets 文件夹复制到外部 SD 卡文件系统的第一个代码 fragment 中
关于android - 如何在android studio中刷新 Assets 文件列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43674045/