2

I'm trying to copy a large number of folders and subfolders from my apk asset folder to my sd card quickly. the folders contain 12mb of small files, probably 2500 total. The code example from this SO question works but it takes over 5 minutes on my device. Is there a faster way to do this?

I originally tried adding the folder to a zip archive and unzipping it after it was moved onto the device but it created to many problems on different devices and was failing a lot throughout the process.

Community
  • 1
  • 1
Peter
  • 5,071
  • 24
  • 79
  • 115
  • 1
    And what about read files from an archive file instead of a tree of dir and subdirs? (ie a zip with no compression). In that case, you have only a file to copy. – Daniele Vrut Jul 25 '13 at 15:55
  • @DJHell That is a great idea. Half of the files are png images the other half are text files. How would i reference the location of those files through my code. Im assuming `Environment.getExternalStorageDirectory() + File.separator + "appFolder" + File.separator + "files.zip" + File.separator + "image.png"` would not work – Peter Jul 25 '13 at 15:58
  • @MiroMarkarian I'm building this application for sale on Google Play so that is not an option to tell users, Also my phone does not have an sd card but an emulated one. – Peter Jul 25 '13 at 15:59
  • 1
    a simple getFileFromMyZippedFile(String filePath) maybe with a cache, but as I said IMHO. – Daniele Vrut Jul 25 '13 at 16:01

2 Answers2

1

12mb should save a bit faster that, if you are using the methods from the other SO question, try increasing the buffer size in copyFile like so,

private void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[8192]; // 1024 is kind small,..try 8192 or 4096!!
    int read;
    while((read = in.read(buffer)) != -1){
      out.write(buffer, 0, read);
    }
}
Community
  • 1
  • 1
petey
  • 16,914
  • 6
  • 65
  • 97
  • Will increasing that buffer size cause issues on lower end devices? – Peter Jul 25 '13 at 16:24
  • if it too big yes.... but I use 8192 (which should be just fine for lower end) ever since the thunderbolt came out for most of my stuff cuz of this blunder htc made. http://stackoverflow.com/questions/5358014/android-httpclient-oom-on-4g-lte-htc-thunderbolt – petey Jul 25 '13 at 16:25
  • My app supports android 2.2 and up, although only 1.5% are 2.2 the rest are 2.3.3 and up (almost 65% are 4.0 and up). Do you think i will cause any issues for the 2.2 devices doing this? From what i can remember the thunderbolt is 2.2 or 2.3? – Peter Jul 25 '13 at 16:55
  • no. thunderbolt started out at 2.2, I believe its current andriod version is 4.03. the 8192 byte buffer size should be good on almost all devices (compared to 1024) – petey Jul 26 '13 at 16:29
1

I've had very good, consistent results with creating a zip file, putting it in raw or assets in my app, and unzipping it when the user first opens the app. I'd recommend you give it another shot, as I've been impressed that I've seen zero issues with hundreds of installs.

The tutorials I based the helper methods to zip and unzip files are here: Unzipping Files w/ Android, and Zipping Files w/ Android

It should be noted that I used the Java API to create the zip that I include with the install of my app. That might be why I had such consistent results unzipping them as well, using the Java API in Android.

Hope this helps! Best of luck!

spierce7
  • 14,797
  • 13
  • 65
  • 106