/**
* 選擇獲取本地圖片還是拍照
*/
private void touxiangUpdate()
{
AlertDialog.Builder builder = new AlertDialog.Builder(context);
String[] picture = { "拍照", "從手機中選擇" };
builder.setTitle("選擇要添加的圖片");
id = 0;
builder.setSingleChoiceItems(picture, id,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,
int whichButton)
{
switch (whichButton)
{
case 0:
id = 0;
doTakePhoto();
break;
case 1:
id = 1;
doSelectImageFromLoacal();
break;
default:
break;
}
dialog.dismiss();
}
});
builder.create().show();
}
/**********************************************拍照裁剪圖片******************************************************/
private static final String IMAGE_FILE_LOCATION = "file:///sdcard/temp.jpg";// temp
private Uri cameraImgUri = Uri.parse(IMAGE_FILE_LOCATION);
private final int CAMERA_WITH_DATA = 1001;
private final int PHOTO_PICKED_WITH_DATA = 1002;
private final int CROP_PICTURE = 1003;
private Bitmap bitMap = null;
private String strResult;
private void doTakePhoto()
{
LogUtil.log("=doTakePhoto=");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraImgUri);
((MainActivity) context).startActivityForResult(intent,
CAMERA_WITH_DATA);
}
private void doSelectImageFromLoacal()
{
LogUtil.log("=doSelectImageFromLoacal=");
Intent openAlbumIntent = new Intent(Intent.ACTION_PICK);
openAlbumIntent.setDataAndType(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
((Activity) context).startActivityForResult(openAlbumIntent,
PHOTO_PICKED_WITH_DATA);
}
// the method of how to get photos from taking or searching
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
LogUtil.log("=onActivityResult=");
if (resultCode != Activity.RESULT_OK)
{
return;
}
switch (requestCode)
{
case PHOTO_PICKED_WITH_DATA:
LogUtil.log("=onActivityResult=PHOTO_PICKED_WITH_DATA");
if (data == null)
{
return;
}
Uri selectedImageUri = data.getData();
if (selectedImageUri != null)
{
startPhotoZoom(selectedImageUri);
}
break;
case CAMERA_WITH_DATA:
LogUtil.log("=onActivityResult=CAMERA_WITH_DATA");
startPhotoZoom(cameraImgUri);
break;
case CROP_PICTURE:
LogUtil.log("=onActivityResult=CROP_PICTURE");
setBitmap(data);
break;
}
}
public void setBitmap(Intent data)
{
LogUtil.log("=setBitmap=");
if (Integer.parseInt(VERSION.SDK) < 14)
{
if (data == null)
{
return;
}
Bundle extra = data.getExtras();
Bitmap lbitmap = null;
if (extra != null)
{
lbitmap = (Bitmap) extra.get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
lbitmap.compress(Bitmap.CompressFormat.JPEG, 30, stream);
}
if (lbitmap == null)
{
Uri photoUri = data.getData();
if (photoUri != null)
{
lbitmap = BitmapFactory.decodeFile(photoUri.getPath());
}
}
bitMap = lbitmap;
}
else
{
Bitmap lbitmap = BitmapFactory.decodeFile(cameraImgUri.getPath());
bitMap = lbitmap;
}
LogUtil.log("=bigmap=" + bitMap);
uploadPhoto(Constant.REQUEST_STATUS.TYPE_USER_TOUXIANG_UPDATE,true);
}
/**
* 拍照裁剪圖片
* @param uri
*/
@SuppressWarnings("deprecation")
public void startPhotoZoom(Uri uri)
{
LogUtil.log("=startPhotoZoom=");
LogUtil.log("=uri=" + uri);
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// 下面這個crop=true是設置在開啓的Intent中設置顯示的VIEW可裁剪
intent.putExtra("crop", "true");
// aspectX aspectY 是寬高的比例
// intent.putExtra("aspectX", 1);
// intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪圖片寬高
if (RBApplication.getIns().getPixels() == Constant.PIXELS_CONSTANT.IS1080P)
{
intent.putExtra("outputX", 720);
intent.putExtra("outputY", 1280);
}
else if (RBApplication.getIns().getPixels() == Constant.PIXELS_CONSTANT.IS720P)
{
intent.putExtra("outputX", 720);
intent.putExtra("outputY", 1280);
}
else
{
intent.putExtra("outputX", 400);
intent.putExtra("outputY", 800);
}
intent.putExtra("scale", true);
if (Integer.parseInt(android.os.Build.VERSION.SDK) < 14)
{
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
intent.putExtra("return-data", true);
}
else
{
intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraImgUri);
intent.putExtra("return-data", false);
}
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
((MainActivity) context).startActivityForResult(intent, CROP_PICTURE);
}
/**
* 上傳圖片
* @param type
*/
private void uploadPhoto(final int type,boolean b)
{
if (!DeviceUtil.isNetworkConnected(context))
{
ToastDialogUtil.getIns().showToast("沒有網絡!");
return;
}
if (b)
{
MainActivity.sendHandlerMessage(
Constant.REQUEST_STATUS.MSG_SEND_REQUEST, null);
}
// ((BaseActivity) context).disProgress();
// ((BaseActivity) context).showProgress();
// 新建線程
new Thread()
{
@Override
public void run()
{
Message msg = new Message();
// headPhoto.setImageBitmap(null);
// bitmap = getHttpBitmap(Constant.Head_Url +
// userHeadImageUrl);
try
{
HttpPost httppost = new HttpPost(
Constant.REQUEST_URI.IMG_URI);
JSONObject jsonObject = new JSONObject();
try
{
jsonObject.put("tranCode",
Constant.REQUEST_URI.USER_TOUXIANG_UPDATE);
JSONObject data = new JSONObject();
data.put("uid", MediaCenter.getIns().getUser().getId());
data.put("token", MediaCenter.getIns().getUser()
.getToken());
jsonObject.put("data", data);
}
catch (Exception e)
{
}
Map<String, Object> map = new HashMap<String, Object>();
map.put("params", jsonObject.toString());
ByteArrayBody arrayBody = null;
MultipartEntity reqEntity = new MultipartEntity();
for (String key : map.keySet())
{
String paramValue = String.valueOf(map.get(key));
reqEntity.addPart(key, new StringBody(paramValue,
Charset.forName("UTF-8")));
}
byte[] bytes = new byte[1024];
if (bitMap != null)
{
ByteArrayOutputStream os = new ByteArrayOutputStream();
bitMap.compress(CompressFormat.JPEG, 50, os);// 壓縮圖片
bytes = os.toByteArray();
arrayBody = new ByteArrayBody(bytes,
System.currentTimeMillis() + ".jpg");
reqEntity.addPart("uploadFile", arrayBody);
}
httppost.setEntity(reqEntity);
HttpResponse httpResponse = new DefaultHttpClient()
.execute(httppost);
// 若狀態碼爲200 ok
if (httpResponse.getStatusLine().getStatusCode() == 200)
{
// 讀返回數據
strResult = EntityUtils.toString(httpResponse
.getEntity());
// // System.out.println(strResult);
// msg.what = 0x1225;
// handler.sendMessage(msg);
if (ParseUtil.parse(type, strResult, false))
{
LogUtil.log("=parse=true");
MainActivity.sendHandlerMessage(type, strResult);
}
else
{
LogUtil.log("=parse=false=");
MainActivity.sendHandlerMessage(
Constant.REQUEST_STATUS.MSG_SEND_FALIURE,
type);
}
}
else
{
// System.out.println("Error Response: "
// + httpResponse.getStatusLine()
// .toString());
// msg.what = 0x11;
// handler.sendMessage(msg);
LogUtil.log("getStatusCode = "
+ httpResponse.getStatusLine().getStatusCode());
MainActivity.sendHandlerMessage(
Constant.REQUEST_STATUS.MSG_SEND_FALIURE, type);
}
}
catch (Exception e)
{
// e.printStackTrace();
// msg.what = 0x11;
// handler.sendMessage(msg);
LogUtil.log("e.getMessage() 2 = " + e.getMessage());
MainActivity.sendHandlerMessage(
Constant.REQUEST_STATUS.MSG_REQUEST_TIMEOUT, "");
e.printStackTrace();
}
}
}.start();
}
圖片上傳
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.