The image is stored in a blob in the background database, and the Android client reads the display

Source: Internet
Author: User

Workaround:

1: Read the picture from the database in the background in a inputstream way:

 Public StaticInputStream Getpicinputstream () {String ID= "F304733361E243779B2340AFE20E62BF"; Connection Conn=jdbcutil.getconnection (); PreparedStatement PS=NULL; ResultSet RS=NULL; InputStream is=NULL; String SQL= "Select ZP from Pic_test where id=?"; Try{PS=conn.preparestatement (SQL); Ps.setstring (1, id); RS=Ps.executequery (); if(Rs.next ()) { is= Rs.getblob ("ZP"). Getbinarystream (); }                    } Catch(SQLException ex) {Logger.getlogger (Test.class. GetName ()). log (Level.severe,NULL, ex); }finally{            Try {                if(NULL!=PS)                {Ps.close (); }            } Catch(SQLException ex) {Logger.getlogger (Test.class. GetName ()). log (Level.severe,NULL, ex); }            Try {                if(NULL!=RS)                {Rs.close (); }            } Catch(SQLException ex) {Logger.getlogger (Test.class. GetName ()). log (Level.severe,NULL, ex);        } jdbcutil.release (conn); }                         returnis ; }

2: Create a picture directly in the servlet:

Response.setcontenttype ("Image/jpeg"); InputStream is=Test.getpicinputstream (); if(NULL!=is ) {outputstream OS=Response.getoutputstream (); intLen; byteBuf[] =New byte[1024];  while((Len=is.read (BUF))!=-1) {os.write (buf,0, Len);            } is.close ();        Os.close (); }

Processing of 3:android Clients

 Public classMainactivityextendsActivity {ImageView imgview; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); Imgview=(ImageView) Findviewbyid (R.id.imgview); NewImgviewasynctask (). Execute (""); }    classImgviewasynctaskextendsAsynctask<string,string,string>{Bitmap Bitmap=NULL; @Overrideprotected voidOnPreExecute () {} @Overrideprotectedstring Doinbackground (String ... strings) {InputStream is=NULL; String result= ""; Try{URL Picurl=NewURL ("Http://192.168.0.165:8084/wisdompm/PicServlet"); HttpURLConnection Conn=(HttpURLConnection) picurl.openconnection (); Conn.setconnecttimeout (10000);                Conn.connect (); is=Conn.getinputstream (); if(NULL!=is ) {Bitmap=Bitmapfactory.decodestream (IS); }            } Catch(malformedurlexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }finally {                Try {                    if(NULL!=is )                    {Is.close (); }                } Catch(IOException e) {e.printstacktrace (); }            }            returnresult; } @Overrideprotected voidOnPostExecute (String s) {if(NULL!=bitmap &&NULL!=Imgview)            {Imgview.setimagebitmap (bitmap); }        }    }}

Test results:

The image is stored in a blob in the background database, and the Android client reads the display

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.