First to download Base64.java file http://iharder.sourceforge.net/current/java/base64/
Copy the code into project.
Then on the code:
Android Side code:
private void Setpictoview (Intent picdata) {
Bundle extras = Picdata.getextras ();
if (extras! = null) {
Mbitmap = extras.getparcelable ("Data");
View_images.setimagebitmap (MBITMAP);
Logutil.i ("Run Reg", "Run Now?") ");
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
The Bitmap-byte throttle output Bitmap.CompressFormat.PNG compression format, 100: Compression ratio. BAOs: Byte stream
Mbitmap.compress (Bitmap.CompressFormat.PNG, BAOs);
try {
Baos.close ();
} catch (IOException e) {
E.printstacktrace ();
}
byte[] buffer = Baos.tobytearray ();
Logutil.i ("Picture size", buffer.length+ "");
Encrypt the picture's byte stream data into a base64 character output
Photo = Base64.encodebytes (buffer);
}
}
Server-side code:
public static void Saveimages (String photo,string filePath) {
String imageName = new Iptimestamp (). Getiptimestamp () + ". png";
try {
Decodes the base64 data to generate a byte array.
byte[] photoimg = new Base64decoder (). Decodebuffer (photo);
for (int i=0;i<photoimg.length;i++) {
if (photoimg[i]<0) {
Adjust exception data
Photoimg[i] + = 256;
}
}
Sysutil.sysout ("Size of the Picture:" + photoimg.length);
File File = new file (filepath,imagename); Create a directory to write a picture inside
if (!file.exists ()) {
File.createnewfile (); File.mkdirs () Create a directory, File.createnewfile () Create a file
}
FileOutputStream out = new FileOutputStream (file);
Out.write (PHOTOIMG);
Out.flush ();
Out.close ();
} catch (Exception e) {
Todo:handle exception
}
Base64 achieve Android image upload to server side