Reputation:
I have a problem.
I want to copy file from assets forder(Path: Assets/Manual) to another directory(Environment.DIRECTORY_DOWNLOADS).
so I write source code like below.
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);
}
}
It does work!
But, when I replace file(Same file instead of diffrent file name) in assets folder(Path: Assets/Manual), this source code logged all previous file name list.
ex)
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"
There is no AAA.pdf in Asset Folder NOW!
How can I treat this issue?
I don't need previous file name list(AAA.pdf)....
Help me Please..
Upvotes: 2
Views: 1221
Reputation: 629
You cannot copy files into the assets folder. In the first code snippet you copied from the assets folder into the external sd card file system
Upvotes: 1