btn_camera = (Button) findViewById(R.id.btn_camera);      btn_photo = (Button) findViewById(R.id.btn_photo);     @Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent 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;}}@Overrideprotected 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 stubFile 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();}

更多相关文章

  1. Android TextView中插入图片
  2. Android 调用系统的照相,浏览图片,转存并裁剪!
  3. android实现涂鸦,保存涂鸦后的图片,清屏
  4. Android Bluetooth 文件接收路径修改方法
  5. Android图片缓存加强版(LruCache+DiskLruCache+软引用)
  6. Android文件图片上传的详细讲解(三)---模式回调类
  7. android保存图片到图库
  8. Android的GridView控件点击图片变暗效果
  9. Android 分别使用Post与Get实现网络图片加载

随机推荐

  1. Unity3D 调用Android原生方法2
  2. Android四种布局详解
  3. 视频教程-OpenCV人脸标定技术实战 适合iO
  4. Android(安卓)adb push ... Read-only fi
  5. Android界面五种常用布局方式
  6. android创建文件夹和文件
  7. Android关于Activity屏蔽/拦截Home键
  8. 安卓真机appium环境配置
  9. Unity头像上传功能实现 二
  10. android 5.1.1开机优化(framework层)