Batch compression and download of Android Images

Source: Internet
Author: User

From the previous blog.

Previously, when developing Android programs, we encountered a very slow speed of downloading a large number of images. We can use the image compression method to compress all images to improve transmission efficiency.

The server uses the servlet output stream and the Java compression tool zipoutputstream and zipinputstream.

Main server code:

Protected void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {// todo auto-generated method stubresponse. reset (); response. setcontenttype ("text/XML"); response. setheader ("content-disposition", "attachment; filename =" icons.zip ""); // Fixed DOWNLOAD name response. setbuffersize (33554432); // get the file list, which can be selected or read from the database. The dynamically generated report string Path = "E:/TP"; zipoutputstrea M zos = NULL; fileinputstream FCM = NULL; try {file = new file (PATH); // ZOS = new zipoutputstream (response. getoutputstream (); // create the output stream of the compressed file for (string filename: file. list () {// extract the image from the directory file and write the compressed file output stream ZOS in sequence. putnextentry (New zipentry (filename); // Add a file to the output stream of the compressed file. The file is defined as new fileinputstream (path + "/" + filename ); // create the input stream of the local disk and read the Data byte inbuf [] = new byte [4096]; // The cache int Len when reading the data; // represents the read length while (LEN = f Is. Read (inbuf ))! =-1) {// read ZOS successfully. write (inbuf, 0, Len); // write the output stream} ZOS. closeentry (); // close the current input file. close (); // close the input stream. flush (); // The entire compressed file is complete, and the output data is response. getoutputstream (). flush (); response. getoutputstream (). close (); ZOS. close (); ZOS = NULL; system. out. println ("compressed. ");} catch (exception e) {// todo auto-generated catch blocke. printstacktrace ();} finally {// close the unclosed output stream try {If (ZOS! = NULL) ZOS. Close () ;}catch (exception e) {e. printstacktrace ();}}}

Android code:

Package down. load. icons; import Java. io. file; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. inputstream; import java.net. httpurlconnection; import java.net. URL; import Java. util. date; import java.util.zip. zipentry; import java.util.zip. zipinputstream; import android. app. activity; import android. OS. bundle; import android. OS. environment; import android. util. log; import android. view. VI EW; import android. view. view. onclicklistener; import android. widget. button; import android. widget. textview; public class downloadiconsactivity extends activity {public textview t; Public button B; Public button D; Public String sdpath; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); If (environment. getexternalstoragestate (). EQ Uals (environment. media_mounted) {sdpath = environment. getexternalstoragedirectory () + "/"; log. V ("SD", sdpath);} t = (textview) findviewbyid (R. id. t); B = (button) findviewbyid (R. id. b); B. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {// todo auto-generated method stubdate d = new date (); T. settext ("" + D. gethours () + "Hour" + D. getminutes () + "Minute" + D. getseconds () + "seconds"); If (EN Vironment. getexternalstoragestate (). equals (environment. media_mounted) {httpurlconnection Hc = NULL; try {// open an HTTP connection url = new URL ("http: // 192.168.0.100: 8080/downloadimages/downloadimgszip "); hc = (httpurlconnection) URL. openconnection (); // defines the input stream inputstream instream = HC. getinputstream (); new file (sdpath + "imgs_down "). mkdir (); zipinputstream inputzip = new zipinputstream (instream); zipentr Y zip = NULL; byte [] outbuf = new byte [4096]; int outlen; file image = NULL; while (ZIP = inputzip. getnextentry ())! = NULL) {image = new file (sdpath + "/imgs_down/" + Zip. getname (); image. createnewfile (); fileoutputstream out = new fileoutputstream (image); While (outlen = inputzip. read (outbuf ))! =-1) {out. write (outbuf, 0, outlen);} Out. flush (); out. close ();} HC. disconnect ();} catch (exception e) {log. V ("Err", "Download error! "+ E. getmessage ();} finally {If (HC! = NULL) HC. disconnect (); Date d_sd = new date (); T. settext (T. gettext () + "--" + d_sd.gethours () + "Hour" + d_sd.getminutes () + "Minute" + d_sd.getseconds () + "second") ;}} else {T. settext ("no memory card") ;}}); button d = (button) findviewbyid (R. id. d); D. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {// todo auto-generated method stubdate date_f = new date (); T. settext ("" + date_f.gethour S () + "Hour" + date_f.getminutes () + "Minute" + date_f.getseconds () + "second"); httpurlconnection Hc = NULL; file TP = NULL; try {// open an HTTP connection URL url = new URL ("http: // 192.168.0.127: 8080/downloadimages/TP/tp.rar"); Hc = (httpurlconnection) URL. openconnection (); // defines the input stream inputstream instream = HC. getinputstream (); TP = new file (sdpath + "tykmimg_down/" + "tp.rar"); TP. createnewfile (); fileoutputstream Fos = new Fileoutputstream (TP); int file_len; byte [] Buf = new byte [1024]; while (file_len = instream. read (BUF, 0, 1024)> 0) {FOS. write (BUF, 0, file_len);} FOS. flush (); FOS. close ();} catch (exception e) {log. V ("Err", "Download error! "+ E. getmessage ();} finally {If (HC! = NULL) HC. disconnect (); Date date_s = new date (); T. settext (T. gettext () + "--" + date_s.gethours () + "Hour" + date_s.getminutes () + "Minute" + date_s.getseconds () + "second ");}}});} public file createsddir (string dirname) {file dir = new file (sdpath + dirname); system. out. println ("storage device's state:" + environment. getexternalstoragestate (); If (environment. getexternalstoragestate (). equals (environment. media_mounted) {system. out. println ("this directory real path is:" + dir. getabsolutepath (); system. out. println ("the result of making Directory:" + dir. mkdir ();} return dir;} public file createsdfile (string filename) throws ioexception {file = new file (sdpath + filename); file. createnewfile (); Return file ;}

'

After testing, it takes only 40 seconds to download more than 700 images.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.