0

I'm using the following snippet of code to copy an item in my assets directory across to the SD card.

String DATA_PATH = Environment.getExternalStorageDirectory().toString() + "/MyApp/";
        String lang = "eng";
        String[] paths = new String[]
                {DATA_PATH, DATA_PATH + "mydata/"};

        for (String path : paths)
        {
            File dir = new File(path);
            if (!dir.exists())
            {
                if (!dir.mkdirs())
                {
                    Log.d(TAG, "ERROR: Creation of directory " + path + " on sdcard failed");
                   // return null;
                }
                else
                {
                    Log.d(TAG, "Created Directory " + path + " on sd card");
                }
            }
            Log.d(TAG, "Path " + path + "already exists, not creating");
        }

        if (!(new File(DATA_PATH + "mydata/" + lang + ".dat")).exists())
        {
            try
            {
                Log.d(TAG, "Couldn't find any dat files on SD card, attempting to copy new ones");
                AssetManager assetManager = getAssets();

                String[] filesList = assetManager.list("");
                StringBuilder sb = new StringBuilder();
                for (String s : filesList)
                {
                    Log.d(TAG, "Found file : " + s);
                    if (s.contains("dat"))
                    {
                        try
                        {
                            Log.d(TAG, "Found a dat file, attempting to copy to SD card");
                            InputStream in = assetManager.open("eng.dat");
                            Log.d(TAG, "Opened an inputStream to eng.dat");
                            // GZIPInputStream gin = new GZIPInputStream(in);
                            OutputStream out = new FileOutputStream(DATA_PATH + "mydata/eng.dat");
                            Log.d(TAG, "Opened an outputStream to " + DATA_PATH + "mydata/eng.dat");

                            // Transfer bytes from in to out
                            byte[] buf = new byte[1024];
                            int len;
                            // while ((lenf = gin.read(buff)) > 0) {
                            while ((len = in.read(buf)) != -1)
                            {
                                out.write(buf, 0, len);
                            }
                            in.close();
                            // gin.close();
                            out.close();

                            Log.d(TAG, "Copied " + lang + " .dat");

                        }
                        catch (IOException ioe)
                        {
                            Log.e(TAG, "Was unable to copy " + s + " " + ioe);
                        }
                    }
                    Log.d(TAG, "File : " + s + " is not something we're interested in, not copying to SD card.");
                }
            }
            catch (IOException e)
            {
                Log.e(TAG, "Was unable to copy assets " + e.toString());
            }
        }

I also have this line in my manifest file :

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

The output of my logcat is :

05-22 13:57:57.289: D/SEARCH(14609): Path /mnt/sdcard/MyApp/already exists, not creating
05-22 13:57:57.289: D/SEARCH(14609): Path /mnt/sdcard/MyApp/mydata/already exists, not creating
05-22 13:57:57.289: D/SEARCH(14609): Couldn't find any traindeddata files on SD card, attempting to copy new ones
05-22 13:57:57.449: D/SEARCH(14609): Found file : badges
05-22 13:57:57.449: D/SEARCH(14609): File : badges is not something we're interested in, not copying to SD card.
05-22 13:57:57.449: D/SEARCH(14609): Found file : eng.dat
05-22 13:57:57.449: D/SEARCH(14609): Found a dat file, attempting to copy to SD card
05-22 13:57:57.449: D/SEARCH(14609): Opened an inputStream to eng.dat
05-22 13:57:57.469: D/SEARCH(14609): Opened an outputStream to /mnt/sdcard/MyApp/mydata/eng.dat
05-22 13:57:57.469: E/SEARCH(14609): Was unable to copy eng.dat java.io.IOException
05-22 13:57:57.469: D/SEARCH(14609): File : eng.dat is not something we're interested in, not copying to SD card.
05-22 13:57:57.469: D/SEARCH(14609): Found file : images
05-22 13:57:57.469: D/SEARCH(14609): File : images is not something we're interested in, not copying to SD card.
05-22 13:57:57.469: D/SEARCH(14609): Found file : sounds
05-22 13:57:57.469: D/SEARCH(14609): File : sounds is not something we're interested in, not copying to SD card.
05-22 13:57:57.469: D/SEARCH(14609): Found file : webkit
05-22 13:57:57.469: D/SEARCH(14609): File : webkit is not something we're interested in, not copying to SD card.

It seems to create the file successfully on the SD card, but it is always 0 bytes. I have around 700MB free space on the card, and my dat file is only 3MB so no capacity issues.

I'm using API level 8.

Can anyone offer a suggestion as to what might be causing the IOException?

Thanks

EDIT: This is the stack trace (thanks for the heads up Narek)

05-22 14:28:37.043: E/INITIALISATION(22774): java.io.IOException
05-22 14:28:37.043: E/INITIALISATION(22774):    at android.content.res.AssetManager.readAsset(Native Method)
05-22 14:28:37.043: E/INITIALISATION(22774):    at android.content.res.AssetManager.access$700(AssetManager.java:36)
05-22 14:28:37.043: E/INITIALISATION(22774):    at android.content.res.AssetManager$AssetInputStream.read(AssetManager.java:571)
05-22 14:28:37.043: E/INITIALISATION(22774):    at com.example.myapp.task.InitialisationTask.doInBackground(InitialisationTask.java:86)
05-22 14:28:37.043: E/INITIALISATION(22774):    at com.example.myapp.task.InitialisationTask.doInBackground(InitialisationTask.java:19)
05-22 14:28:37.043: E/INITIALISATION(22774):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
05-22 14:28:37.043: E/INITIALISATION(22774):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-22 14:28:37.043: E/INITIALISATION(22774):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-22 14:28:37.043: E/INITIALISATION(22774):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
05-22 14:28:37.043: E/INITIALISATION(22774):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
05-22 14:28:37.043: E/INITIALISATION(22774):    at java.lang.Thread.run(Thread.java:1096)
Jimmy
  • 16,123
  • 39
  • 133
  • 213
  • firsly change line Log.e(TAG, "Was unable to copy " + s + " " + ioe); to Log.e(TAG, "Was unable to copy " + s, ioe); – narek.gevorgyan May 22 '12 at 13:17
  • @narek.gevorgyan thanks, I've pulled in the trace now – Jimmy May 22 '12 at 13:27
  • Just for a lark, try changing the file ending from ".dat" to something that the asset manager won't attempt to compress into sh*t - e.g. call it "`eng.mp3`". Bugs are fun. – Jens May 22 '12 at 14:57
  • @Jens you were right, if you put that as an answer I'll accept since it solved my problem :) – Jimmy Jun 05 '12 at 23:40

1 Answers1

0

James.Elsey!I read your code,I think your problem in the graph of the red circle of this place:

byte[] buf = new byte[1024]; 
int len; 
// while ((lenf = gin.read(buff)) > 0) { 
while ((len = in.read(buf)) != -1) { 
    out.write(buf, 0, len); 
    out.flush();//Refresh the buffer output stream.This forced all buffer output byte iswrite to the bottom of the output stream. 
} 
in.close(); 
// gin.close(); 
out.close(); 

You can try to solve your problem. My English is not good, don't know if I put the question made myself clear. Flush() method about the more detailed explanation you can go to the reference API documentation.

Jens
  • 16,853
  • 4
  • 55
  • 52
Inferjay
  • 1
  • 3
  • Nothing to do with my actual code, turns out there is a restriction on filesize of assets. I've added a `.mp3` extension as suggested here : http://stackoverflow.com/questions/1273300/ioexception-while-reading-from-inputstream – Jimmy May 22 '12 at 14:54