Androidはサーバーに画像をアップロードします
5411 ワード
btn_camera = (Button) findViewById(R.id.btn_camera);
btn_photo = (Button) findViewById(R.id.btn_photo);
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent;
switch (v.getId()) {
case R.id.btn_photo:
intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image/*");
startActivityForResult(intent, 1);
break;
case R.id.btn_camera:
intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
//
picPath = FileUtils.getPhotoFileName();
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri
.fromFile(new File(Environment
.getExternalStorageDirectory()+PublicVariable.PIC_PATH,picPath)));
startActivityForResult(intent, 2); //pic (/.test/pictures/"),picPath("542212121.jpg")
break;
default:
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
//
case 1:
if (data != null)
startPhotoZoom(data.getData());
// iv_Portrait.setImageURI(data.getData());// ,
break;
//
case 2:
File temp = new File(Environment.getExternalStorageDirectory()
+ PublicVariable.PIC_PATH,picPath);
startPhotoZoom(Uri.fromFile(temp));//
break;
case 3:
if (data != null) {
setPicToView(data);
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
/**
*
*
* @param uri
*/
public void startPhotoZoom(Uri uri) {
/*
* Intent ACTION ,
* yourself_sdk_path/docs/reference/android/content/Intent.html
* Ctrl+F :CROP , , , , C C++
* , , ...
*/
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
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
startActivityForResult(intent, 3);
}
/**
*
*
* @param picdata
*/
private void setPicToView(Intent picdata) {
Bundle extras = picdata.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
ImageUtil.WriteBitmapToSdCard(Environment.getExternalStorageDirectory()
+ PublicVariable.PIC_PATH, picPath, photo);// sd
Log.i("tcp", Environment.getExternalStorageDirectory()
+ PublicVariable.PIC_PATH + picPath);
File file = new File(
Environment.getExternalStorageDirectory()
+ PublicVariable.PIC_PATH + picPath);
Log.i("tcp", file.exists()+"");
ContentBody cbFile = new FileBody(file);
MultipartEntity mpEntity = new MultipartEntity(); //
mpEntity.addPart("photoFile", cbFile);
mpEntity.addPart("name", new StringBody(foodName, Charset.forName("UTF-8")));
mpEntity.addPart("rstId", new StringBody(restId));
mpEntity.addPart("rstPhotoType", new StringBody(photoType));
// mpEntity.addPart("score", new StringBody(total_score.getProgress()+""));
// mpEntity.addPart("price", new StringBody(et_cost.getText().toString()));
HashMap<String, String> resp = SingleHttpClient.getInstance().getResponse(
getApplicationContext(), mpEntity,
"/mobile/uploadRstPhoto");
if(resp!=null&&resp.get("code").equals("0")){
handler.sendEmptyMessage(0);
}else{
handler.sendEmptyMessage(-1);
}
*/
}
}
//
public static void WriteBitmapToSdCard(String path, String fileName,
Bitmap bitmap) throws IOException {
// TODO Auto-generated method stub
File dirFile = new File(path);
if (!dirFile.exists()) {
dirFile.mkdir();
}
File myCaptureFile = new File(path + fileName);
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(myCaptureFile));
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos); //ok, /sdcard/feng.png
bos.flush();
bos.close();
}