android 上传图片到服务器

      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();
	}

猜你喜欢

转载自284772894.iteye.com/blog/1832041