1

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)
Community
  • 1
  • 1
Master
  • 2,847
  • 4
  • 30
  • 63

4 Answers4

2

Have you given permission in manifest.

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

because writing something to sd need permission

  • 1
    This is the first thing that I did. I am not getting exception because of this. There is some other reason. – Master Jan 11 '14 at 07:34
  • I have added log trace in the original question. – Master Jan 11 '14 at 07:43
  • have you check the file size of .tar file in your asset manager folder. As i know we can copy only file size less than 1 Mb from asset file manager. Visit http://stackoverflow.com/questions/2860157/load-files-bigger-than-1m-from-assets-folder – Praween Kumar Mishra Jan 11 '14 at 08:12
  • Check for size of tar file.is it less than 1MB because android have size restriction in asset folder also check if there is fixed extension that are supported in asset folder – Akhil Dad Jan 11 '14 at 08:16
0

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

Keerthivasan
  • 12,040
  • 2
  • 26
  • 49
  • I have posted a link from which I am using the code. On that link the accepted answer is what I am using as my code to copy file from assets. – Master Jan 11 '14 at 08:05
0

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();
    }
}
Rick Falck
  • 1,748
  • 3
  • 15
  • 18
  • Thanks for your answer. I got it where the problem was. Source of the problem was file extension. – Master Jan 11 '14 at 09:36
0

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.

Master
  • 2,847
  • 4
  • 30
  • 63