コード例 #1
0
  public void activityResult(int requestCode, int resultCode, Intent data) {
    // 判断是否操作的当前的控件,如果不是直接返回。
    if (this.tag != CURRENT_TAG) return;

    if (requestCode == FLAG_CHOOSE_IMG && resultCode == Activity.RESULT_OK) { // 选择图片
      if (data != null) {
        Uri uri = data.getData();
        if (!TextUtils.isEmpty(uri.getAuthority())) {
          Cursor cursor =
              context
                  .getContentResolver()
                  .query(uri, new String[] {MediaStore.Images.Media.DATA}, null, null, null);
          if (null == cursor) {
            Toast.makeText(context, "图片没找到", Toast.LENGTH_SHORT).show();
            return;
          }
          cursor.moveToFirst();
          String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
          cursor.close();
          Log.i("===", "path=" + path);
          Intent intent = new Intent(context, CropImageActivity.class);
          intent.putExtra("path", path);
          context.startActivityForResult(intent, FLAG_MODIFY_FINISH);
        } else {
          Log.i("===", "path=" + uri.getPath());
          Intent intent = new Intent(context, CropImageActivity.class);
          intent.putExtra("path", uri.getPath());
          context.startActivityForResult(intent, FLAG_MODIFY_FINISH);
        }
      }
    } else if (requestCode == FLAG_CHOOSE_PHONE && resultCode == Activity.RESULT_OK) { // 拍照
      File f = new File(FILE_PIC_SCREENSHOT, localTempImageFileName);
      Intent intent = new Intent(context, CropImageActivity.class);
      intent.putExtra("path", f.getAbsolutePath());
      context.startActivityForResult(intent, FLAG_MODIFY_FINISH);

    } else if (requestCode == FLAG_MODIFY_FINISH && resultCode == Activity.RESULT_OK) {
      if (data != null) {
        final String path = data.getStringExtra("path");
        Log.i("===", "截取到的图片路径是 = " + path);

        Bitmap b = BitmapFactory.decodeFile(path);

        headerImageStr = bitmap2Base64(b);

        if (this.listener != null) {
          this.listener.uploadImage(headerImageStr);
        }
      }
    }
  }
コード例 #2
0
 public void takePhoto() {
   String status = Environment.getExternalStorageState();
   if (status.equals(Environment.MEDIA_MOUNTED)) {
     try {
       localTempImageFileName = "";
       localTempImageFileName = String.valueOf((new Date()).getTime()) + ".png";
       File filePath = FILE_PIC_SCREENSHOT;
       if (!filePath.exists()) {
         filePath.mkdirs();
       }
       Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
       File f = new File(filePath, localTempImageFileName);
       // localTempImgDir和localTempImageFileName是自己定义的名字
       Uri u = Uri.fromFile(f);
       intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
       intent.putExtra(MediaStore.EXTRA_OUTPUT, u);
       context.startActivityForResult(intent, FLAG_CHOOSE_PHONE);
     } catch (ActivityNotFoundException e) {
       e.printStackTrace();
     }
   }
 }