七牛客户端上传图片文件

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
百度网盘地址
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值