@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode){
case REQUST_CODE_ZOOM:
if(data==null){
return;
}
Bundle extras = data.getExtras();
Bitmap bm = extras.getParcelable("data");
imgHead.setImageBitmap(bm);
try {
uploadByAsyncClient(WebApiConfig.URL_UPLOAD_HEAD,bm);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
break;
case REQUST_CODE_IMG:
if(data==null){
return;
}
Uri uri = data.getData();
Uri fileUri = converUri(uri);
startImageZoom(fileUri);
break;
}
}
}
/**
* 保存图像到SD卡中
* @param bm
* @return
*/
private Uri saveBitmap(Bitmap bm){
File tmpDir = new File(Environment.getExternalStorageDirectory()+"/com.imgHead.header");
if(!tmpDir.exists()){
tmpDir.mkdir();
}
File img = new File(tmpDir.getAbsolutePath()+"/header.png");
try {
FileOutputStream fos = new FileOutputStream(img);
bm.compress(Bitmap.CompressFormat.PNG, 85, fos);
fos.flush();
fos.close();
return Uri.fromFile(img);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 将content类型转为file类型的uri
* @param uri
* @return
*/
private Uri converUri(Uri uri){
InputStream is = null;
try {
is = getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(is);
is.close();
return saveBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 图像裁剪
*/
private void startImageZoom(Uri uri){
Intent intentzoom = new Intent("com.android.camera.action.CROP");
intentzoom.setDataAndType(uri, "image/*");
intentzoom.putExtra("crop", "true");
intentzoom.putExtra("aspectX", 1);
intentzoom.putExtra("aspectY", 1);
intentzoom.putExtra("outputX",150);
intentzoom.putExtra("return-data",true);
startActivityForResult(intentzoom,REQUST_CODE_ZOOM);
}
/**
* 由AsyncHttpClient框架上传
*/
private void uploadByAsyncClient(String url,Bitmap bm) throws FileNotFoundException{
showLoadingDialog(mContext);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 60, stream);
byte[] bytes = stream.toByteArray();
String img = new String(com.loopj.android.http.Base64.encodeToString(bytes, com.loopj.android.http.Base64.DEFAULT));
RequestParams params = new RequestParams();
params.put("image", img);
client.post(url, params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
dialog.cancel();
Log.d("car","转码:"+ Base64.encodeToString(responseBody, Base64.DEFAULT));
String response = writeFile(responseBody);
ToastUtils.show(mContext, "成功啦!" + response);
try {
JSONObject object = new JSONObject(response);
String successCode = object.getString("success");
if (TextUtils.equals(successCode, "0")) {
String url = object.getString("url");
commitUserLogo(url);
} else {
ToastUtils.show(mContext, "上传图片失败!");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
dialog.cancel();
ToastUtils.show(mContext, "失败啦!" + responseBody.toString());
}
});
}