android解压缩文件内存泄漏

时间:2013-12-03 01:25:45

标签: android memory unzip

我有下面的解压缩文件代码。 (来自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();
}

解压缩文件没问题。但随着我的计划继续运行。它试图通过下面的代码显示解压缩的jpeg图像(1000px x 800px)。我创建了一个按钮,一次显示一个图像。

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

加载第一个图像没有问题,但是当我按下下一个按钮时,它试图加载下一个图像,它称为内存不足异常。 我想知道我的解压缩代码是否有内存泄漏?

0 个答案:

没有答案