I am trying to copy a .tar file from android assets to sdcard but while copying file I am getting IOException. I am using this code from a previous thread Android: How to copy files in 'assets' to sdcard?
Here is LogCat file. I am doing all this in ASyncTask but I have also tried it on main UI thread and still getting this exception.
01-11 06:51:49.925: E/tag(3881): Failed to copy asset file: temp.tar
01-11 06:51:49.925: E/tag(3881): java.io.IOException
01-11 06:51:49.925: E/tag(3881): at android.content.res.AssetManager.readAsset(Native Method)
01-11 06:51:49.925: E/tag(3881): at android.content.res.AssetManager.access$700(AssetManager.java:36)
01-11 06:51:49.925: E/tag(3881): at android.content.res.AssetManager$AssetInputStream.read(AssetManager.java:571)
01-11 06:51:49.925: E/tag(3881): at com.example.apptest.MainActivity.copyFile(MainActivity.java:130)
01-11 06:51:49.925: E/tag(3881): at com.example.apptest.MainActivity.copyAssets(MainActivity.java:116)
01-11 06:51:49.925: E/tag(3881): at com.example.apptest.MainActivity.access$0(MainActivity.java:97)
01-11 06:51:49.925: E/tag(3881): at com.example.apptest.MainActivity$1.doInBackground(MainActivity.java:32)
01-11 06:51:49.925: E/tag(3881): at com.example.apptest.MyASyncTask.doInBackground(MyASyncTask.java:1)
01-11 06:51:49.925: E/tag(3881): at android.os.AsyncTask$2.call(AsyncTask.java:185)
01-11 06:51:49.925: E/tag(3881): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-11 06:51:49.925: E/tag(3881): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-11 06:51:49.925: E/tag(3881): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
01-11 06:51:49.925: E/tag(3881): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
01-11 06:51:49.925: E/tag(3881): at java.lang.Thread.run(Thread.java:1096)
解决方案
Actually, I think the main problem is with zip or tar format. It can not copy when you provide this kinda file format to copy from assets to sdcard. Reason might be that zip is in itself a collection of files which creates problem.
But changing the file extension I am able to copy that .tar file to sdcard from assets.
The best idea is to keep your file without extension in assets and while writing it to output stream append extension with its name(preceding dot). Then copying this file will be not hinderance anymore.