Android java压缩文件并使用intent

时间:2016-01-24 16:55:10

标签: java android zip

我有这种方法从List中压缩文件,另一种方法使用它来通过意图发送邮件。

我的问题是,当我发送两到三次应用程序崩溃并向我显示时。

E/StrictMode: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
                                                                 java.lang.Throwable: Explicit termination method 'close' not called
at dalvik.system.CloseGuard.open(CloseGuard.java:184)
at java.io.FileOutputStream.<init>(FileOutputStream.java:89)
at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
at com.waffles.vatsandbats.VisaDatai.zip(VisaDatai.java:1172)
at com.waffles.vatsandbats.VisaDatai.sendZippedMail(VisaDatai.java:207)
at com.waffles.vatsandbats.VisaDatai.getFiles(VisaDatai.java:298)
at com.waffles.vatsandbats.VisaDatai$7$1.run(VisaDatai.java:1823)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5373)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

主要问题(我认为)在此消息中

at com.waffles.vatsandbats.VisaDatai.zip(VisaDatai.java:1172)

指的是这个

in = new FileInputStream(files.get(i)
                .getCanonicalFile());

以下是创建zip并具有错误代码

的方法
public static File zip(List<File> files, String filename) {
    File zipfile = new File(filename);
    // Create a buffer for reading the files
    FileInputStream in=null;
    byte[] buf = new byte[1024];
    try {
        // create the ZIP file
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
                zipfile));
        // compress the files
        for (int i = 0; i < files.size(); i++) {
             in = new FileInputStream(files.get(i)
                    .getCanonicalFile());
            // add ZIP entry to output stream
            out.putNextEntry(new ZipEntry(files.get(i).getName()));
            // transfer bytes from the file to the ZIP file
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            // complete the entry
            out.closeEntry();
            in.close();
        }
        // complete the ZIP file
        out.close();
        return zipfile;
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return null;
}

我压缩的文件列表是几个带有图像和文本的PrintedPdfDocuments(这个类有它的缺点,但我现在懒得改变它)

我只是找不到问题。也许我需要改变拉链的方法。有什么建议吗?

2 个答案:

答案 0 :(得分:0)

您应该在finally块中关闭流,以确保即使发生异常也能正确关闭它们。

在创建getCanonicalFile()时,使用FileInputStream也会创建新文件。你可能想要:

in = new FileInputStream(files.get(i));

答案 1 :(得分:0)

我解决了。我关闭了for循环中的FileInputStream。因此,每次循环并打开流时,我也关闭它。

我最后添加了一个自己的try catch for close block,但是当我拿走for循环中的close部分时它就崩溃了。

这是工作代码

public  File zip(List<File> files, String filename) {
    File zipfile = new File(filename);
    FileInputStream in=null;
    ZipOutputStream out=null;
    // Create a buffer for reading the files
    byte[] buf = new byte[1024];
    try {
        // create the ZIP file
        out = new ZipOutputStream(new FileOutputStream(
                zipfile));
        // compress the files
        for (int i = 0; i < files.size(); i++) {

            in = new FileInputStream(files.get(i));

            // add ZIP entry to output stream
            out.putNextEntry(new ZipEntry(files.get(i).getName()));
            // transfer bytes from the file to the ZIP file
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            try {
                in.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }


    } catch (IOException ex) {
        ex.printStackTrace();
    }finally {

        try {
            try {
                in.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }

            out.closeEntry();

            out.close();
        }catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    return zipfile;
}