【问题标题】:Android Full Image From Crop Intent来自裁剪意图的 Android 完整图像
【发布时间】:2015-01-05 14:41:56
【问题描述】:

我正在使用 Android 手机上的相机拍摄图像。拍摄图像后,我将完整图像作为 uri 传递给裁剪意图 (com.android.camera.action.CROP)。但是,当裁剪意图返回时,它会以缩略图大小返回包裹。我试图将相机中的图像保存在一个文件中并发送文件的 uri,但它没有在裁剪意图中打开图像。如何从它发回的包裹中获取我在裁剪意图中裁剪的全尺寸图像?这是我正在使用的一些代码:

public void onActivityResult(int requestCode, int resultCode,
        Intent intent) {
    if (resultCode != Activity.RESULT_CANCELED) {
        Uri uri = null;
        switch (requestCode) {
        case FILE_SELECT_CODE:
            if (intent.getData() != null) {
                uri = intent.getData();
                cropSelectedPicture(uri);
            }
            break;
        case PICTURE_SELECT_CODE:
            uri = intent.getData();
            if (uri == null) {
                Bundle bundle = intent.getExtras();
                Bitmap photo = null;
                if (bundle != null) {
                    photo = bundle.getParcelable("data");
                }
                finishChangeProfilePicture(photo);
            } else {
                cropSelectedPicture(uri);
            }
            break;
        case FILE_CROP_CODE:
            Bundle extras = intent.getExtras();
            Bitmap thePic = extras.getParcelable("data");
            uri = extras.getParcelable("uri");
            finishChangeProfilePicture(thePic);
            break;
        }
    }
    super.onActivityResult(requestCode, resultCode, intent);
}

public void showCamera() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra("crop", "true");
    intent.putExtra("outputX", pictureWidth);
    intent.putExtra("outputY", pictureHeight);
    intent.putExtra("aspectX", pictureWidth);
    intent.putExtra("aspectY", pictureHeight);
    intent.putExtra("scale", true);
    intent.putExtra("return-data", true);
    try {
        cordova.startActivityForResult(
                this, Intent.createChooser(intent, "Take Picture"),
                PICTURE_SELECT_CODE);
    } catch (android.content.ActivityNotFoundException ex) {
        // Potentially direct the user to the Market with a Dialog
        Toast.makeText(context, "Please install a Camera application.", 
                Toast.LENGTH_SHORT).show();
    }
}

public void cropSelectedPicture(Uri uri) {
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(uri, "image/*");
    intent.putExtra("crop", "true");
    intent.putExtra("outputX", pictureWidth);
    intent.putExtra("outputY", pictureHeight);
    intent.putExtra("aspectX", pictureWidth);
    intent.putExtra("aspectY", pictureHeight);
    intent.putExtra("scale", true);
    intent.putExtra("return-data", true);

    PackageManager packageManager = context.getPackageManager();
    List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
    if (activities.size() > 0) {
        for (int i = 0; i < activities.size(); i++) {
            String label = (String) activities.get(i).loadLabel(packageManager);
            if (label.equals("Crop picture")) {
                ActivityInfo activity = activities.get(i).activityInfo;
                ComponentName name = new ComponentName (activity.applicationInfo.packageName,
                                                        activity.name);
                intent.setComponent(name);
            }
        }
    }

    try {
        cordova.startActivityForResult(this, intent, FILE_CROP_CODE);
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(context, "Please install a Cropping program", Toast.LENGTH_SHORT).show();
    }
}

【问题讨论】:

  • Android 没有CROP Intent: commonsware.com/blog/2013/01/23/…
  • @CommonsWare 我已经看到你在这个问题上发布的博文。我没有看到任何建议——在您帖子中的那些库之外。安装图库时,不存在的裁剪意图可以正常工作。但是,当它使用 Google+ 照片应用时,它就崩溃了,什么也没有发生。
  • @KickingLettuce:“安装图库时,不存在的裁剪意图可以正常工作”——并且当设备制造商或 ROM 修改者未修改图库时,只要Gallery 应用程序选择维护这个未记录和不受支持的 API,只要用户选择 Gallery 应用程序。而且并非所有设备上都存在 Gallery 应用,取而代之的是 Google+ 照片(如您所述)或其他应用(应设备制造商或 ROM 改装者的要求)。
  • @CommonsWare 我明白了。在非常特定的情况下,它工作正常。不幸的是,这种情况越来越少。那么有什么替代方案呢?
  • Pick a library。我个人不需要一个,所以我没有尝试过。我的博客文章中的那些是在搜索时出现的;此评论中的链接是用于图像裁剪的 Android Arsenal 类别。可能还有其他人。我只是试图引导人们不要使用不可靠的东西,而不是引导人们使用其他特定的解决方案。

标签: android image crop


【解决方案1】:

好的,所以我必须做的是从相机意图中保存 uri,然后在脱离裁剪意图时将图像保存到该意图。我之前尝试过一次,但它是应用程序的内部文件而不是内容解析器。 (公平警告:这是一个 cordova 项目,而不是原生 android 项目,所以如果您在使用 Media.getBitmap 行时遇到问题,请删除 cordova.getActivity() 部分,这应该可以正常工作)这是我所做的更改:

public void onActivityResult(int requestCode, int resultCode,
        Intent intent) {
    if (resultCode != Activity.RESULT_CANCELED) {
        switch (requestCode) {
        case FILE_SELECT_CODE:
            if (intent.getData() != null) {
                uri = intent.getData();
                cropSelectedPicture();
            }
            break;
        case PICTURE_SELECT_CODE:
            uri = intent.getData();
            if (uri == null) {
                Bundle bundle = intent.getExtras();
                Bitmap photo = null;
                if (bundle != null) {
                    photo = bundle.getParcelable("data");
                }
                finishChangeProfilePicture(photo);
            } else {
                cropSelectedPicture();
            }
            break;
        case FILE_CROP_CODE:
            Bitmap theUriPic;
            try {
                theUriPic = Media.getBitmap(cordova.getActivity().getContentResolver(), uri);
                finishChangeProfilePicture(theUriPic);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            break;
        }
    }
    super.onActivityResult(requestCode, resultCode, intent);
}

public void cropSelectedPicture() {
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(uri, "image/*");
    intent.putExtra("crop", "true");
    intent.putExtra("outputX", pictureWidth);
    intent.putExtra("outputY", pictureHeight);
    intent.putExtra("aspectX", pictureWidth);
    intent.putExtra("aspectY", pictureHeight);
    intent.putExtra("scale", true);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    intent.putExtra("return-data", true);

    PackageManager packageManager = context.getPackageManager();
    List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
    if (activities.size() > 0) {
        for (int i = 0; i < activities.size(); i++) {
            String label = (String) activities.get(i).loadLabel(packageManager);
            if (label.equals("Crop picture")) {
                ActivityInfo activity = activities.get(i).activityInfo;
                ComponentName name = new ComponentName (activity.applicationInfo.packageName,
                                                        activity.name);
                intent.setComponent(name);
            }
        }
    }

    try {
        cordova.startActivityForResult(this, intent, FILE_CROP_CODE);
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(context, "Please install a Cropping program", Toast.LENGTH_SHORT).show();
    }
}

【讨论】:

    猜你喜欢
    • 2015-02-02
    • 2012-01-16
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 2014-07-25
    • 1970-01-01
    • 2016-05-04
    • 1970-01-01
    相关资源
    最近更新 更多