private Uri imageUri;//打开照相机private void openCamera() { Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); imageUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"image.jpg")); //指定照片保存路径(SD卡),image.jpg为一个临时文件,每次拍照后这个图片都会被替换 openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);startActivityForResult(openCameraIntent, CAMERA_REQUEST_CODE);}//打开图库private void openPhones() {    // 图库选择    // 激活系统图库,选择一张图片    Intent intent_gallery = new Intent(Intent.ACTION_PICK);    intent_gallery.setType("image/*");    startActivityForResult(intent_gallery, IMAGE_REQUEST_CODE);}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {    super.onActivityResult(requestCode, resultCode, data);    if (requestCode == IMAGE_REQUEST_CODE) {//相册        imageUri= data.getData();        //获取照片路径        String[] filePathColumn = {MediaStore.Audio.Media.DATA};        Cursor cursor = getContentResolver().query(imageUri, filePathColumn, null, null, null);        cursor.moveToFirst();        photoPath = cursor.getString(cursor.getColumnIndex(filePathColumn[0]));        cursor.close();        crop(imageUri);    } else if (requestCode == CAMERA_REQUEST_CODE) {//相机        photoPath = Environment.getExternalStorageDirectory() + "/image.jpg";        crop(imageUri);    } else if (requestCode == RESULT_REQUEST_CODE) {        setImageBitmap();        img_touxiang.setImageBitmap(bitmap);        bitmapToString = bitmapToString(bitmap);       //请求服务器的接口,上传到服务器    }} /** * 剪切图片 */private void crop(Uri uri) {    // 裁剪图片意图    Intent intent = new Intent("com.android.camera.action.CROP");    intent.setDataAndType(uri, "image/*");    intent.putExtra("crop", "true");    // 裁剪框的比例,1:1    intent.putExtra("aspectX", 1);    intent.putExtra("aspectY", 1);    // 裁剪后输出图片的尺寸大小    intent.putExtra("outputX", 250);    intent.putExtra("outputY", 250);    // 图片格式    intent.putExtra("outputFormat", "PNG");    intent.putExtra("noFaceDetection", true);// 取消人脸识别    intent.putExtra("return-data", true);// true:不返回uri,false:返回uri    startActivityForResult(intent, RESULT_REQUEST_CODE);//同样的在onActivityResult中处理剪裁好的图片}/** * 压缩图片 */private void setImageBitmap() {    //获取imageview的宽和高    int targetWidth = img_touxiang.getWidth();    int targetHeight = img_touxiang.getHeight();    //根据图片路径,获取bitmap的宽和高    BitmapFactory.Options options = new BitmapFactory.Options();    options.inJustDecodeBounds = true;    BitmapFactory.decodeFile(photoPath, options);    int photoWidth = options.outWidth;    int photoHeight = options.outHeight;    //获取缩放比例    int inSampleSize = 1;    if (photoWidth > targetWidth || photoHeight > targetHeight) {        int widthRatio = Math.round((float) photoWidth / targetWidth);        int heightRatio = Math.round((float) photoHeight / targetHeight);        inSampleSize = Math.min(widthRatio, heightRatio);    }    //使用现在的options获取Bitmap    options.inSampleSize = inSampleSize;    options.inJustDecodeBounds = false;    bitmap = BitmapFactory.decodeFile(photoPath, options);    img_touxiang.setImageBitmap(bitmap);}//把bitmap转换成字符串public static String bitmapToString(Bitmap bitmap) {    String string = null;    ByteArrayOutputStream btString = new ByteArrayOutputStream();    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, btString);    byte[] bytes = btString.toByteArray();    string = Base64.encodeToString(bytes, Base64.DEFAULT);    return string;}
 
 
 

 

更多相关文章

  1. 图片的放大缩小
  2. android 强制修改adb pull 文件的路径
  3. 关于android 调用系统图片浏览器并返回图片路径问题
  4. Android保存图片到本地
  5. 【Android自学笔记】Android获取手机和存储卡上的图片
  6. android 使用statfs获得文件路径可用空间大小的方法
  7. android中如何显示图片的一部分
  8. android Uri利用及解析(文件操作)以及与路径的相互转换

随机推荐

  1. 浅谈mysql的备份
  2. 1.4.6 收集sql语句的执行计划 2
  3. Python MySQLdb连接数据库的应用
  4. Effective MySQL之深入解析复制技术
  5. 在arcpy中删除或删除表的代码是什么?
  6. oracle11g 创建id自增长监听器的步骤
  7. 在SQL SELECT语句中重用别名字段
  8. 用javabean连接sql server 2000数据库报
  9. CentOS下MySQL主从同步配置 ​Slave_IO_R
  10. 使用IN语句缓慢mysql删除查询