コード例 #1
0
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
      // 访问相册
      if (requestCode == CommonConstant.REQUESTCODE_PHOTO_ALBUM) {
        String fileName = null;
        if (data != null) {
          // 得到图片的全路径
          Uri uri = data.getData();
          ContentResolver cr = getContentResolver();
          Cursor cursor = cr.query(uri, null, null, null, null);
          if (cursor.moveToFirst()) {
            do {
              fileName = cursor.getString(cursor.getColumnIndex("_data"));
            } while (cursor.moveToNext());
          }
          tempFile = new File(fileName);
          if (tempFile.exists() && tempFile.length() > 0) {
            ImageItem albumPic = new ImageItem();
            albumPic.setIsLocalPic(true);
            albumPic.setImagePath(fileName);
            picList.add(albumPic);
            editGridAdapter.notifyDataSetChanged();
          }
        }
      }
      // 访问相机
      if (requestCode == CommonConstant.REQUESTCODE_PHOTO_CAMERA) {
        if (FileUtils.hasSdcard()) {
          Bitmap bitmap = ImageUtils.scale(BitmapFactory.decodeFile(imagePath), 0.7F, 0.7F);
          if (null == imagePath || "".equals(imagePath)) {

          } else {
            tempFile = new File(imagePath);
            if (tempFile.exists() && tempFile.length() > 0) {
              // 判断照片是否被旋转,如果旋转则旋转回0°
              int degree = ImageUtils.getBitmapDegree(imagePath);
              if (degree != 0) {
                bitmap = ImageUtils.rotateBitmapByDegree(bitmap, degree);
              }
              ImageUtils.toFile(bitmap, tempFile);
              ImageItem takePhoto = new ImageItem();
              takePhoto.setIsLocalPic(true);
              takePhoto.setImagePath(imagePath);
              picList.add(takePhoto);
              editGridAdapter.notifyDataSetChanged();
            } else {
            }
          }

        } else {
          ToastUtils.showToast("未找到存储卡,无法存储照片!");
        }
      }
    }
    super.onActivityResult(requestCode, resultCode, data);
  }
コード例 #2
0
 /** @Description: 从拍照获取 */
 private void getPicFromCamera() {
   imagePath = FileUtils.getImagePath();
   File f = new File(imagePath);
   if (f.exists()) {
     f.delete();
   }
   try {
     f.createNewFile();
   } catch (IOException e) {
     e.printStackTrace();
   }
   Uri uri = Uri.fromFile(f);
   Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
   camera.putExtra(MediaStore.EXTRA_OUTPUT, uri);
   startActivityForResult(camera, CommonConstant.REQUESTCODE_PHOTO_CAMERA);
 }