Our project team lead actually wants to convert the image into a binary stream and store it in the database ~~... I'm going... Absolutely disagree. My personal opinion is to upload images to the server and save the image resources to the server disk. The database only stores images in the server path .... But. Ah ~~ The Government has a level of pressure on the dead .. No way ~~ I had to brainstorm for information and finally solve the problem after a day of hard work ~~ Heheh
My idea is to convert an image to a binary stream byte [], then convert byte [] to a string type string, and then transmit it using the stream.
Let's just move on to the code.
First, in the Web Servlet
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Response. setcontenttype ("text/html; charset = UTF-8 ");
Fileinputstream FCM = new fileinputstream ("F:/classical beauty/haha.jpg ");
Bytearrayoutputstream baos = new bytearrayoutputstream ();
Byte [] buffer = new byte [1024];
Int COUNT = 0;
While (COUNT = FS. Read (buffer)> = 0 ){
Baos. Write (buffer );
}
String STR = new string (base64.encode (baos. tobytearray ()));
Printwriter PW = response. getwriter ();
PW. Write (STR );
PW. Flush ();
PW. Close ();
}
This part of the code is located in the dopost method of the Web-side servlet .. I am here for simplicity .. Instead of connecting to the database to get the binary stream of the image, you can use a local image to convert it into a binary array and upload it to the android terminal .. The same is true .. Haha.
The most noteworthy is: String STR = new string (base64.encode (baos. tobytearray (); this code converts a byte [] to a string object using the static method of base64 encoding class encode.
Note .. Although byte [] also has a tostring method that can convert byte [] to a string, it seems that this method has drawbacks. There will be data loss and garbled characters (I tried it .. I don't know what you will do ..) Then write it out with printwriter .....
Okay, there are so many web-side code.
The key is the android code. OK !!! Code on
First, you must add the <uses-Permission Android: Name = "android. Permission. Internet"/> permission to the androidmanifest. xml file on the android client.
Private imageview image = NULL;
Public static string Path = "http: // 10.0.2.2: 8080/conndb/newservlet ";
Private string result = "";
Declare three variables in the activity first.
In oncreate ()
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Image = (imageview) findviewbyid (R. Id. Image );
Showimage ();
}
Let's take a look at the code in the showimage method.
Public void showimage (){
Httpget request = new httpget (PATH );
Try {
// Use response to receive data returned by Servlet
Httpresponse response = new defaulthttpclient(cmd.exe cute (request );
// Determine whether the servlet returns success
If (response. getstatusline (). getstatuscode () = 200 ){
Httpentity entity = response. getentity ();
Bufferedreader BR = new bufferedreader (New inputstreamreader
(Entity. getcontent (), "UTF-8 "));
String line = "";
While (line = Br. Readline ())! = NULL ){
Result = Result + line;
}
System. Out. println (result );
If (! Result. Equals ("")){
Bitmap bitmap = conntestactivity. getbitmapfrombyte (result );
Image. setimagebitmap (Bitmap );
}
}
} Catch (exception e ){
E. printstacktrace ();
}
Result = "";
}
Byte [] is generated by using the passed string. It is used to generate a bitmap object and displayed in the imageview.
Bitmap bitmap = conntestactivity. getbitmapfrombyte (result );
Image. setimagebitmap (Bitmap );
Next let's take a look at the getbitmapfrombyte (result); Method
Public static bitmap getbitmapfrombyte (string Str ){
If (STR = NULL ){
Return NULL;
}
Bitmap bitmap = NULL;
Byte [] bytes = base64.decode (STR );
Bitmap = bitmapfactory. decodebytearray (bytes, 0, bytes. Length );
Return bitmap;
}
Byte [] bytes = base64.decode (STR); this code serves the opposite purpose on the Web end. It converts streing to byte []...
Okay, write the code here .... The next step is to run
Running the android simulator will display
But here is a little bit of a problem... Is the base64 class ....
Base64 is not an API of the Java official class, although this class can be used in java files .. However, if Android is used, an error is reported... This is a headache for me .. As a result, I had to go online to find the encoding class written by others .. Convert it into a class for use in the project. OK to solve the problem ......
If someone needs this encoding class, they can download it to my space and I will upload it... You can also find it on your own .. Hehahaha
Base64.java connection: http://download.csdn.net/my/uploads