Android裁剪图片不能正常工作android-crop

时间:2015-04-07 18:25:42

标签: android image crop onactivityresult

我正在使用库https://github.com/jdamcd/android-crop并且无法获取裁剪后的图像我不知道为什么以下是我从文档中遵循的一些代码

_Profile_Image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent,
                    "Select Picture"), SELECT_PICTURE_FROM_GALARY);
        }
    });
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent result) {
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == SELECT_PICTURE_FROM_GALARY) {

            beginCrop(result.getData());
        }
    }

    if (requestCode == Crop.REQUEST_PICK && resultCode == Activity.RESULT_OK) {
        beginCrop(result.getData());
    } else if (requestCode == Crop.REQUEST_CROP) {
        handleCrop(resultCode, result);
    }
}

private void beginCrop(Uri source) {
    Uri outputUri = Uri.fromFile(new File(getActivity().getCacheDir(), "cropped"));
    new Crop(source).output(outputUri).asSquare().start(getActivity());
}

private void handleCrop(int resultCode, Intent result) {
    if (resultCode == Activity.RESULT_OK) {
        _Profile_Image.setImageURI(Crop.getOutput(result));
    } else if (resultCode == Crop.RESULT_ERROR) {
        Toast.makeText(getActivity(), Crop.getError(result).getMessage(), Toast.LENGTH_SHORT).show();
    }
}

我能够打开公司活动,它还可以裁剪图像并显示保存图片,但问题是在裁剪图像后它没有进入onActivityResult,因此我可以处理裁剪图像。 我不知道我在代码中做错了什么,我只是按照文档

帮助将得到感谢。

1 个答案:

答案 0 :(得分:0)

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class ImageResizer {

 public static Bitmap decodeSampledBitmapFromFile(String filename,
        int reqWidth, int reqHeight) {
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options
            options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filename, options);
    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(filename, options);
}




public static int calculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {
    // BEGIN_INCLUDE (calculate_sample_size)
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }

        // This offers some additional logic in case the image has a strange
        // aspect ratio. For example, a panorama may have a much larger
        // width than height. In these cases the total pixels might still
        // end up being too large to fit comfortably in memory, so we should
        // be more aggressive with sample down the image (=larger inSampleSize).

        long totalPixels = width * height / inSampleSize;

        // Anything more than 2x the requested pixels we'll sample down further
        final long totalReqPixelsCap = reqWidth * reqHeight * 2;

        while (totalPixels > totalReqPixelsCap) {
            inSampleSize *= 2;
            totalPixels /= 2;
        }
    }
    return inSampleSize;
    // END_INCLUDE (calculate_sample_size)
}
}

希望这个帮助:)