从图库中获取图像,但不会裁剪

时间:2014-03-13 12:43:07

标签: android android-intent crop

我有这段代码从图库中检索图像

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("image/*");
    intent.putExtra("crop", "true");
    intent.putExtra("aspectX", 0);
    intent.putExtra("aspectY", 0);
    intent.putExtra("outputX", 360);
    intent.putExtra("outputY", 360);
 try
    {
        intent.putExtra("return-data", true);
        startActivityForResult(Intent.createChooser(intent, "Complete action using"), req_code);
    }
    catch(ActivityNotFoundException e)
    {
        // Do nothing for now
    }

但即使使用intent.putExtra("crop", "true");,在选择图片后,它也不会显示任何作物活动或其他......为什么?

4 个答案:

答案 0 :(得分:1)

因为它不应该。仅仅因为你在随机Intent对象上添加随机内容并不会让第三方应用程序无法做出他们不做的事情。

这是the documentation for ACTION_GET_CONTENT。请注意,您在此处列出的所有附加内容均不在文档中。因此,没有第三方应用程序必然会期望这些额外的东西。

Android没有开发人员可用的内置图像裁剪功能。但是,有很多图像裁剪库可供使用。

答案 1 :(得分:0)

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, SELECT_PHOTO);

答案 2 :(得分:0)

试试这段代码:(用于从图库中获取图片)

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, GALLERY_REQUEST);
            dialog.dismiss();

(从图库中挑选图片后调用此处)

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Bitmap bmp = null;
    if (resultCode == RESULT_OK) {
        try {

            if (requestCode == GALLERY_REQUEST) {
                // Gallery request result
                mImageCaptureUri = data.getData();

                TEMP_PHOTO_FILE_NAME = new File(

                startCropImage();

        } catch (Exception e) {
            e.printStackTrace();
            File f = new File(getRealPathFromURI(mImageCaptureUri));
            if (f.getName().startsWith(FILE_NAME)) {
                if (f.exists())
                    f.delete();
            }
        }
    }
}

private void startCropImage() {


    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setType("image/*");

  /**
     * Check if there is image cropper app installed.
     */
    List<ResolveInfo> list = getPackageManager().queryIntentActivities(
            intent, 0);

    int size = list.size();
            /**
     * If there is no image cropper app, display warning message
     */
    if (size == 0) {

        Toast.makeText(this, "Can not find image crop app",
                Toast.LENGTH_SHORT).show();

        return;
    } else {
        /**
         * Specify the image path, crop dimension and scale
         */
        intent.setData(mImageCaptureUri);

        intent.putExtra("outputX", 256);
        intent.putExtra("outputY", 256);
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("scale", true);
        intent.putExtra("return-data", true);
        /**
         * There is posibility when more than one image cropper app exist,
         * so we have to check for it first. If there is only one app, open
         * then app.
         */

        if (size == 1) {
            Intent i = new Intent(intent);
            ResolveInfo res = list.get(0);

            i.setComponent(new ComponentName(res.activityInfo.packageName,
                    res.activityInfo.name));

            startActivityForResult(i, CROP_FROM_CAMERA);
        }
}

答案 3 :(得分:-1)

protected void onActivityResult(int requestCode, int resultCode,
        Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

    switch (requestCode) {
    case SELECT_PHOTO: //Select photo == 1
        if (resultCode == RESULT_OK) {
            try {
                final Uri imageUri = imageReturnedIntent.getData();
                Bitmap selectedImage = BitmapFactory
                        .decodeStream(getContentResolver().openInputStream(
                                imageUri));