Android应用裁剪照片

时间:2016-11-21 03:24:42

标签: java android crop photo

我有一个问题。 这是我的应用程序已有的代码,它的工作方式如下:拍照> 裁剪> 上传即可。 我不想裁剪照片。我怎么能这样做?只是删除dispatchCropImageIntent方法?

  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(this.getClass().getSimpleName(), "onActivityResult");
    if (resultCode == RESULT_OK) {
        if (requestCode == REQUEST_IMAGE_CAPTURE) {
            File imageFile = new File(mCurrentPhotoPath);
            if (imageFile.exists()) {
               dispatchCropImageIntent(Uri.fromFile(imageFile));
            }
        } else if (requestCode == REQUEST_IMAGE_FROM_GALLERY_SELECT) {
            dispatchCropImageIntent(data.getData());
        } else if (requestCode == REQUEST_PICTURE_CROP) {
            String filePath = Environment.getExternalStorageDirectory()
                    + "/temporary_holder.jpg";
            setCurrentBitmap(BitmapFactory.decodeFile(filePath));
        } else if(requestCode == CAMERA_ACTIVITY_CODE){
            // Get path
            String path = data.getStringExtra(CustomCamera.OUT_PATH);

            // Read file
            byte[] imgData = AppFS.readFileFromPath(path);
            if (imgData != null) {
                Bitmap bitmap = BitmapFactory.decodeByteArray(imgData , 0, imgData.length);
                // TODO: Do something with the image, it should be okay
                //((ImageView)findViewById(R.id.img)).setImageBitmap(bitmap);
            } else {
                Log.e("Main", "Data is null");
            }
        }
    }
    else{
        onBackPressed();
    }
}

并且:

private void dispatchCropImageIntent(Uri uri) {
    Intent cropImageIntent = new Intent("com.android.camera.action.CROP");
    cropImageIntent.setDataAndType(uri, "image/*");
    cropImageIntent.putExtra("crop", "true");
    cropImageIntent.putExtra("noFaceDetection", true);
    cropImageIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
    // retrieve data on return
    cropImageIntent.putExtra("return-data", true);
    File f = new File(Environment.getExternalStorageDirectory(),
            "/temporary_holder.jpg");
    try {
        f.createNewFile();
    } catch (IOException ex) {
        Log.e("io", ex.getMessage());
    }

    uri = Uri.fromFile(f);
    cropImageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    startActivityForResult(cropImageIntent, REQUEST_PICTURE_CROP);
}

1 个答案:

答案 0 :(得分:0)

如果我理解正确,你想让你的应用程序像这样工作:拍摄照片>上传。因此,您需要将 dispatchCropImageIntent()替换为 setCurrentBitmap()上传methond