Android: Copying tar file from assets to sdcard - IOException

自闭症网瘾萝莉.ら 提交于 2019-12-08 07:42:29

问题


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 How to copy files from 'assets' folder 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)

回答1:


Have you given permission in manifest.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

because writing something to sd need permission




回答2:


I don't think the problem is with permission to write. It is when you try to read the file from assets folder. Try cleaning your project and build it again. Please note that you have to use getAssets() in your MainActivity to open the asset file. Use Try/Catch block to catch the IOException to find out the root cause. Please post the appropriate code to help you




回答3:


Here is what I do to copy an XML file from my assets folder to the SD Card:

    File toPath = Environment.getExternalStoragePublicDirectory(mAppDirectory);
    if (!toPath.exists()) {
        toPath.mkdir();
    }

    try {
        InputStream inStream = getAssets().open("file.xml");
        BufferedReader br = new BufferedReader(new InputStreamReader(inStream));
        File toFile = new File(toPath, "file.xml");
        copyAssetFile(br, toFile);
    } catch (IOException e) {
    }

private void copyAssetFile(BufferedReader br, File toFile) throws IOException {
    BufferedWriter bw = null;
    try {
        bw = new BufferedWriter(new FileWriter(toFile));

        int in;
        while ((in = br.read()) != -1) {
            bw.write(in);
        }
    } finally {
        if (bw != null) {
            bw.close();
        }
        br.close();
    }
}



回答4:


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.



来源:https://stackoverflow.com/questions/21059761/android-copying-tar-file-from-assets-to-sdcard-ioexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!