Base64 achieve Android image upload to server side

Source: Internet
Author: User

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

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.