[英]Android java zipping files and send with 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$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());
這是創建郵政編碼並具有錯誤代碼的方法
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(此類具有缺點,但我現在很懶地更改了它)
我就是找不到問題。 也許我需要更改壓縮方法。 有什么建議么?
您應該在finally塊中關閉流,以確保即使發生異常也可以正確關閉流。
在創建FileInputStream
時,還可以使用getCanonicalFile()
創建一個新文件。 您可能想要:
in = new FileInputStream(files.get(i));
我解決了 我在for循環中關閉了FileInputStream。 因此,每次循環並打開流時,我也會關閉它。
最終,為了確定起見,我為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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.