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

标签 android android-studio android-fragments android-camera android-crop

我正在开发一个应用程序,作为其中的一部分,我需要在从相机捕获图像或从图库中选取图像后裁剪图像。我的应用程序中有一个抽屉导航,用户可以从中选择一个选项来查看他的图像(相机或图库)。每当他单击其中一个选项时,将调用一个 fragment ,其余部分将在其中完成(调用相机、单击图片、获取数据并将图像设置到 ImageView )。所以,现在我需要为图像添加裁剪功能,并且我使用了在线可用的裁剪库之一,可以找到 here .在我的应用程序中尝试之前,我创建了一个新的 android studio 项目并制作了一个示例应用程序,用户可以在其中通过单击按钮选择相机,然后完成裁剪并将裁剪后的图像设置为屏幕上的 ImageView 。该项目可以找到here .现在,我试图在我的应用程序中实现相同的功能,但它不起作用。来到代码中,我有一个抽屉导航类,然后是一个 fragment 类。在我的 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( fragment )中:

这是完整的代码:

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);
}
}

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

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

同样的工作在不同的示例应用程序项目中没有任何问题。(它没有 fragment )

请帮我解决这个问题!

最佳答案

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

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

关于Android:在捕获或从图库中选取后裁剪图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39499624/

相关文章:

android - 如何在运行我的 gradle 构建之前在 Android Studio 中设置环境变量

android-studio - 从 Android Studio 运行所有 flutter 单元测试

android - 如何将 Android MVVM 模式与 fragment 一起使用?

android - 带有自动滚动 android 的 Viewpager

android - 崩溃 fragment

android - 即使使用 FragmentTransaction.addToBackStack() 后退按钮关闭应用程序

android - 无法在 Android Studio 中生成已签名的 APK 提示我找不到 proguard-rules.txt 错误?

android - Greenrobot 的 Eventbus : Two Fragments calling async tasks jobs got Eventbus confused

android - 本地数据库与远程 webservice 数据库同步

android - 从 appcompat 中删除未使用的资源