Android java 压缩文件并按意图发送

Android java zipping files and send with intent

我有这种方法可以从列表中压缩文件,还有另一种方法可以使用它通过 intent 通过邮件发送它。

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

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.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(这个 class 有它自己的缺点,但我现在懒得改变它)

我就是找不到问题所在。也许我需要改变压缩的方法。有什么建议吗?

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

在创建 FileInputStream 时还使用 getCanonicalFile() 创建一个新文件。你可能想要:

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

我解决了。我关闭了 for 循环内的 FileInputStream。所以每次它循环并打开流时我也将其关闭。

我在 finally 处为 close 块添加了自己的 try catch 只是为了确定,但是当我在 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;
}