Android: 在捕获或从图库中选取后裁剪图像

Android: Cropping an image after capture or picking from Gallery

我正在开发一个应用程序,作为应用程序的一部分,我需要在从相机捕获图像或从图库中选取图像后裁剪图像。我的应用程序中有一个导航抽屉,用户可以从中 select 选择查看他的图像(相机或画廊)。每当他单击其中一个选项时,将调用一个片段,其余部分将在其中完成(调用相机、单击图片、获取数据并将图像设置到图像视图)。所以,现在我需要为图像添加裁剪功能,并且我使用了在线可用的裁剪库之一,可以找到 here. Before trying it in my app, I have created a new android studio project and made a sample app in which the user can select camera by clicking a button and then cropping is done and the cropped image is set to the imageview on the screen. The project can be found here。现在,我试图在我的应用程序中实现相同的功能,但它不起作用。来到代码中,我有一个导航抽屉 class,然后是一个片段 class。在我的 MainDrawerActivity(简要)中:

在菜单选项中:

if (id == R.id.nav_camera) { 
        Fragment_TouchImageView camFrag = (Fragment_TouchImageView) getSupportFragmentManager().findFragmentById(R.id.imageFragment);
        camFrag.startCamera();
    }
else if (id == R.id.nav_gallery) { 
        Fragment_TouchImageView galFrag = (Fragment_TouchImageView) getSupportFragmentManager().findFragmentById(R.id.imageFragment);
        galFrag.openGallery();
    }

现在,在我的 Fragment_TouchImageView.java(片段)中:

这是完整的代码:

package com.example.shravan.watershed;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.soundcloud.android.crop.Crop;

import java.io.File;

public class Fragment_TouchImageView extends Fragment {

Uri imageUri;
private TouchImageView myTVF;
static final int REQUEST_IMAGE_CAPTURE = 10;
static final int PICK_IMAGE = 100;
private static Context context;
private static final String TAG = "Arunachala";

public Fragment_TouchImageView() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_fragment__touch_image_view, container, false);
    context= getActivity().getApplicationContext();
    myTVF = (TouchImageView) v.findViewById(R.id.img);
    myTVF.setImageBitmap(null);
    return v;
}

public void startCamera() {
    Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(i, REQUEST_IMAGE_CAPTURE);
}

public void openGallery() {
    Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(gallery, PICK_IMAGE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
        print("Camera requested");
        beginCrop(data.getData());
        print("Crop called and came to next line");
    }
    else if (requestCode == PICK_IMAGE  && resultCode == Activity.RESULT_OK)           {
        print("Gallery requested");
        beginCrop(data.getData());
        print("Crop called and came to next line");
    }
    else if (requestCode == Crop.REQUEST_CROP) {
        print("Crop.REQUEST_CROP called");
        handleCrop(resultCode, data);
        print("handleCrop called and came to next line");
    }
}

private void beginCrop(Uri source) {
    print("Crop has begun");
    Uri destination = Uri.fromFile(new File(context.getCacheDir(), "cropped"));
    Crop.of(source, destination).asSquare().start(getActivity());
    print("Crop has ended");
}

private void handleCrop(int resultCode, Intent result) {
    print("Came to handleCrop");
    if (resultCode == Activity.RESULT_OK) {
        print("RESULT OK");
        myTVF.setImageURI(Crop.getOutput(result));
    } else if (resultCode == Crop.RESULT_ERROR) {
        Toast.makeText(getActivity(), Crop.getError(result).getMessage(), Toast.LENGTH_SHORT).show();
    }
}

private void print(String s){
    Log.d(TAG, s);
}
}

所以,我给出了一系列的日志标签来了解流程,这就是问题所在。每当我 select 相机或画廊时,它都会正确打开,当我捕获图像时,会调用 beginCrop() 并且当我 select 所需的裁剪区域并单击完成时,它什么也不做。流程不会进入此循环:

else if (requestCode == Crop.REQUEST_CROP) {
        print("Crop.REQUEST_CROP called");
        handleCrop(resultCode, data);
        print("handleCrop called and came to next line");
    }

在不同的示例应用程序项目中同样可以正常工作。(它没有片段)

请帮我解决这个问题!

几周前我在使用同一个库时遇到了同样的问题,所以问题出在 onActivityResult 及其在片段中使用它时的 requestCode 所以尝试这样调用 Crop.of:

Crop.of(cropInputUri, cropOutputUri).asSquare().start(getContext(), Fragment.this, Crop.REQUEST_CROP);