Get picture display and upload in assets directory

Source: Internet
Author: User

1  Packagecom.jingle.getlocal;2 3 ImportJava.io.ByteArrayOutputStream;4 5 ImportJava.io.InputStream;6 7 ImportOrg.apache.http.Header;8 9 ImportSun.misc.BASE64Encoder;Ten  One Importcom.loopj.android.http.AsyncHttpClient; A ImportCom.loopj.android.http.AsyncHttpResponseHandler; - ImportCom.loopj.android.http.RequestParams; -  the  - Importandroid.app.Activity; - ImportAndroid.content.res.AssetManager; - ImportAndroid.graphics.Bitmap; + Importandroid.graphics.BitmapFactory; -  + ImportAndroid.os.Bundle; A ImportAndroid.util.Log; at ImportAndroid.view.View; - ImportAndroid.view.View.OnClickListener; - ImportAndroid.widget.Button; - ImportAndroid.widget.ImageView; -  -  Public classMainactivityextendsActivity { in     PrivateString upload_url = "HTTP://192.168.1.101:8080/LOGIN//SERVLET/UPLOADIMG"; -  to @Override +     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); the Setcontentview (r.layout.activity_main); *  $ initimg ();Panax Notoginseng initbtnuploadimg (); -  the     } +  A     Private voidinitbtnuploadimg () { theButton BTN =(Button) Findviewbyid (R.ID.BTN); +Btn.setonclicklistener (NewOnclicklistener () { -  $              Public voidOnClick (View v) { $ uploadimg (); -             } -         }); the     } - Wuyi     Private voiduploadimg () { theAsynchttpclient client =Newasynchttpclient (); -Client.settimeout (3000); WuRequestparams params =Newrequestparams (); -         //Get Bitmap Object AboutBitmap bmp = Getimg ("Orca.jpg"); $         //get byte array output stream -Bytearrayoutputstream BAOs =NewBytearrayoutputstream (); -         //compress Bitmap object to byte array output stream -Bmp.compress (Bitmap.CompressFormat.JPEG, 90, BAOs); A         //flow byte array output to byte array +         byte[] ba =Baos.tobytearray (); the         //encode a byte array into a string -String str =NewBase64encoder (). Encode (BA); $Params.put ("img", str); the  theClient.post (Upload_url, params,NewAsynchttpresponsehandler () { the              Public voidOnsuccess (intARG0, header[] arg1,byte[] arg2) { theLOG.I ("Ace", "Post OK ..."); -             } in  the              Public voidOnFailure (intARG0, header[] arg1,byte[] arg2, the throwable Arg3) { About arg3.printstacktrace (); the             } the         }); the  +     } -  the     Private voidinitimg () {BayiImageView img =(ImageView) Findviewbyid (r.id.img); theImg.setimagebitmap (getimg ("orca.jpg"));//Assets directory under file name the     } -  -     //get the Bitmap object for a picture the     PrivateBitmap getimg (String file) { theBitmap BMP =NULL; the         //Get Assetsmng Object theAssetmanager am =getresources (). Getassets (); -         Try { the             //open file, return input stream theInputStream is =am.open (file); the             //Bitmap Factory decodes input stream to get BMP object94BMP =Bitmapfactory.decodestream (IS); the is.close (); the}Catch(Exception e) { the e.printstacktrace ();98         } About         returnbmp; -     }101 102}

Server-side:

1  PackageAction;2 3 ImportJava.io.FileOutputStream;4 Importjava.io.IOException;5 ImportJava.io.OutputStream;6 ImportJava.io.PrintWriter;7 8 Importjavax.servlet.ServletException;9 ImportJavax.servlet.http.HttpServlet;Ten Importjavax.servlet.http.HttpServletRequest; One ImportJavax.servlet.http.HttpServletResponse; A  - ImportSun.misc.BASE64Decoder; -  the  -  -  -  Public classUploadimgextendsHttpServlet { +  -     Private Static Final LongSerialversionuid = -8236824851962797021l; +  A      Public voiddoget (httpservletrequest request, httpservletresponse response) at             throwsservletexception, IOException { -Response.setcontenttype ("text/html"); - DoPost (request, response); -     } -  -      Public voidDoPost (httpservletrequest request, httpservletresponse response) in             throwsservletexception, IOException { -          toSYSTEM.OUT.PRINTLN ("do post.."); +String imgstr = Request.getparameter ("img"); -         //decoding a string to a byte array the         byte[] Imgbyte =NewBase64decoder (). Decodebuffer (IMGSTR); *         //new file output stream $OutputStream OS =NewFileOutputStream ("E:\\up.jpg");Panax Notoginseng         //output stream write byte array - Os.write (imgbyte); the os.close (); +          APrintWriter out =Response.getwriter (); the Out.flush (); + out.close (); -     } $  $}

Get picture display and upload in assets directory

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.