Android java zipping files and send with intent -


i have method zips files list , method uses send mail through intent.

my problem when send 2 or 3 times app crashes , shows me this.

e/strictmode: resource acquired @ attached stack trace never released. see java.io.closeable information on avoiding resource leaks.                                                                  java.lang.throwable: explicit termination method 'close' not called @ dalvik.system.closeguard.open(closeguard.java:184) @ java.io.fileoutputstream.<init>(fileoutputstream.java:89) @ java.io.fileoutputstream.<init>(fileoutputstream.java:72) @ com.waffles.vatsandbats.visadatai.zip(visadatai.java:1172) @ com.waffles.vatsandbats.visadatai.sendzippedmail(visadatai.java:207) @ com.waffles.vatsandbats.visadatai.getfiles(visadatai.java:298) @ com.waffles.vatsandbats.visadatai$7$1.run(visadatai.java:1823) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:211) @ android.app.activitythread.main(activitythread.java:5373) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1020) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:815) 

the main problem (i think) in message

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

that refers this

in = new fileinputstream(files.get(i)                 .getcanonicalfile()); 

here's method creates zip , has error code

public static file zip(list<file> files, string filename) {     file zipfile = new file(filename);     // create buffer reading files     fileinputstream in=null;     byte[] buf = new byte[1024];     try {         // create zip file         zipoutputstream out = new zipoutputstream(new fileoutputstream(                 zipfile));         // compress files         (int = 0; < files.size(); i++) {              in = new fileinputstream(files.get(i)                     .getcanonicalfile());             // add zip entry output stream             out.putnextentry(new zipentry(files.get(i).getname()));             // transfer bytes file zip file             int len;             while ((len = in.read(buf)) > 0) {                 out.write(buf, 0, len);             }             // complete entry             out.closeentry();             in.close();         }         // complete zip file         out.close();         return zipfile;     } catch (ioexception ex) {         ex.printstacktrace();     }     return null; } 

the list of files zip several printedpdfdocuments images , texts (this class has owns disadvantages im lazy changing right now)

i can't find problem. maybe need change method zips. suggestions?

you should close streams in block make sure closed when exception occurs.

also using getcanonicalfile() creates new file when creating fileinputstream. want:

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

Comments