sd卡java.util,安卓:从复制资产SD卡tar文件 - IOException异常

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值