Seven Ox client upload picture file

Source: Internet
Author: User

Package Com.sdnu.jimbo.qiniu;import Java.io.bufferedreader;import Java.io.bytearrayoutputstream;import Java.io.inputstreamreader;import Org.apache.http.httpresponse;import Org.apache.http.client.httpclient;import Org.apache.http.client.methods.httpget;import Org.apache.http.impl.client.defaulthttpclient;import Org.json.jsonobject;import Com.qiniu.android.http.responseinfo;import Com.qiniu.android.storage.upcompletionhandler;import Com.qiniu.android.storage.uploadmanager;import Android.os.bundle;import Android.util.base64;import Android.util.log;import Android.app.activity;import Android.graphics.bitmap;import Android.graphics.bitmap.compressformat;import Android.graphics.drawable.bitmapdrawable;import android.graphics.drawable.drawable;/** * @author Jimbo * * 1.                 Import two jar Packages--Qiniu-android-sdk-version.jar * android-async-http-version.jar//This jar is Android an open source * Framework for network requests * 2. Do some processing of your uploaded images such as converting to a byte array is generally converted to base64 better * 3. Get Uoloadtoken * 4. New One UploadmanaGER, call the Put method, upload the resource * 5. Determine if the upload is successful//You can also define other more complex actions such as what the breakpoint continues to */public class Mainactivity extends Activity {String url = "";//server Generation Uploadtoken ur        L @Override protected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); String Uoloadtoken = Getuploadtoken ();//Get uploadtoken byte[] result = Getbaseecode ();//Get the BASE64 encoding of the picture Uploadm Anager UploadManager = new UploadManager ()///based on API Uploadmanager.put provided by seven kn (result, uoloadtoken, NULL, new Upcompleti  Onhandler () {@Overridepublic void complete (String arg0, Responseinfo arg1, Jsonobject arg2) {//TODO auto-generated method    STUBLOG.I ("Qiniu", arg1.tostring ());}}, NULL);        }private byte[] Getbaseecode () {drawable mydrawable = this.getresources (). getdrawable (R.drawable.likeadog);         Bitmapdrawable bdrawable = (bitmapdrawable) mydrawable;        Bitmap Mybitmap = Bdrawable.getbitmap (); Bytearrayoutputstream OutputStream =New Bytearrayoutputstream ();        Mybitmap.compress (Compressformat.jpeg, OutputStream);        Byte[] B = Outputstream.tobytearray ();        Byte[] result = Base64.encode (b, Base64.default); return result;}    Private String Getuploadtoken () {HttpClient client = new Defaulthttpclient ();     StringBuilder builder = new StringBuilder ();    HttpGet myget = new HttpGet (URL);        try {httpresponse response = Client.execute (Myget);        BufferedReader reader = new BufferedReader (New InputStreamReader (Response.getentity (). GetContent ()));        for (String s = reader.readline (); s! = null; s = Reader.readline ()) {builder.append (s);    } return builder.tostring ();        } catch (Exception e) {log.i ("url response", "false");        E.printstacktrace ();    return null; }}}
SOURCE Download Link: http://pan.baidu.com/s/1c0gzpkc Password: XWDF
Baidu Network Address

Seven Ox client upload picture file

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.