0

I have below code for unzip file. (from a thread in stackoverflow)

/**
 * Unzip a zip file.  Will overwrite existing files.
 * 
 * @param zipFile Full path of the zip file you'd like to unzip.
 * @param location Full path of the directory you'd like to unzip to (will be created if it doesn't exist).
 * @throws IOException
 */
public static void unzip(String zipFile, String location, String excludePath) throws IOException {
    int size;
    byte[] buffer = new byte[BUFFER_SIZE];

    try {
        if ( !location.endsWith("/") ) {
            location += "/";
        }
        File f = new File(location);
        if(!f.isDirectory()) {
            f.mkdirs();
        }
        FileInputStream fin = new FileInputStream(zipFile);
        BufferedInputStream bin = new BufferedInputStream(fin, BUFFER_SIZE);
        ZipInputStream zin = new ZipInputStream(bin);
        try {
            ZipEntry ze = null;
            while ((ze = zin.getNextEntry()) != null) {
                //String path = location + ze.getName();
                String unzipFilePath = ze.getName().replace(excludePath, "");
                String path = location + unzipFilePath;
                File unzipFile = new File(path);

                if (ze.isDirectory()) {
                    if(!unzipFile.isDirectory()) {
                        unzipFile.mkdirs();
                    }
                } else {
                    // check for and create parent directories if they don't exist
                    File parentDir = unzipFile.getParentFile();
                    if ( null != parentDir ) {
                        if ( !parentDir.isDirectory() ) {
                            parentDir.mkdirs();
                        }
                    }

                    // unzip the file
                    FileOutputStream out = new FileOutputStream(unzipFile, false);
                    BufferedOutputStream fout = new BufferedOutputStream(out, BUFFER_SIZE);

                    try {
                        while ( (size = zin.read(buffer, 0, BUFFER_SIZE)) != -1 ) {
                            fout.write(buffer, 0, size);
                        }

                        zin.closeEntry();
                    }
                    finally {
                        fout.flush();
                        fout.close();
                        out.close();
                    }
                }
            }
        }
        finally {
            zin.close();
            fin.close();
            bin.close();
        }
    }       
    catch (Exception e) {
        Log.e("bug", "Unzip exception", e);
    }
    buffer = null;
    System.gc();
}

I have no problem in unzipping file. But as my program continue to run. It tried to show the unzipped jpeg image ( 1000px x 800px) by below code. I created a button to show one image at a time.

                    fisImg = new FileInputStream(new File(imgPath[i]));
                    Bitmap imgBitmap = BitmapFactory.decodeStream(fisImg );

It has no problem in loading the first image, but when i pressed next button, it tried to load the next image, it called out of memory exception. I wonder if my unzip code has memory leakage?

manhon
  • 683
  • 7
  • 27
  • 1
    A `1000x800px` images needs roughly 3MB of memory (4 byte per pixel). If you load multiple images and keep them in memory you can easily run out of memory. I would suspect you problem there because the unzip code looks fine. Do some [memory analysis](http://android-developers.blogspot.de/2011/03/memory-analysis-for-android.html) if you want to find out what the problem actually is. – zapl Dec 03 '13 at 01:34
  • But i did not get memory leakage problem if i did not call unzip file. That is, I stored the images via DDMS to sdcard and hard code the image paths. That why I suppose the problem caused by unzip. – manhon Dec 03 '13 at 02:43
  • i will try memory analysis and update the thread soon. thanks – manhon Dec 03 '13 at 02:53
  • you r right, bitmap loading used up memory, finally, i followed http://stackoverflow.com/questions/11820266/android-bitmapfactory-decodestream-out-of-memory-with-a-400kb-file-with-2mb-f and is ok now. So, the max memory allowed per app in android is 32mb? – manhon Dec 04 '13 at 11:38
  • The limit is dynamic and depends on how much RAM your device has (they started with like 128MB in total) – zapl Dec 04 '13 at 12:04

0 Answers0