七牛客户端上传图片文件

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. 导入两个jar包 -- qiniu-android-sdk-VERSION.jar
 *                 android-async-http-VERSION.jar//这个jar是android一个开源的
 *                 //网络请求的框架
 * 2. 对你的上传的图片进行一定的处理 比如转换成byte数组 一般转换成base64比较好
 * 3. 获得uoLoadToken
 * 4. new一个UploadManager,调用put方法,上传资源
 * 5. 判断是否上传成功 //还可以定义其他的一些更复杂的动作 比如断点续传什么的
 */


public class MainActivity extends Activity {
	String url = "";//服务器生成upLoadToken的URL
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String uoLoadToken = getUploadToken();//获取upLoadToken
        byte[] result = getBaseEcode();//获取图片的Base64编码
        UploadManager uploadManager = new UploadManager();//根据七牛提供的api
        uploadManager.put(result, uoLoadToken, null, new UpCompletionHandler() {
			
			@Override
			public void complete(String arg0, ResponseInfo arg1, JSONObject arg2) {
				// TODO Auto-generated method stub
				Log.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, 100, 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;
	    }
	}
}
//源码下载 链接:http://pan.baidu.com/s/1c0gzpkc 密码:xwdf
百度网盘地址

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。