裁剪图像与谷歌的照片 [英] Cropping image with google photos

查看:236
本文介绍了裁剪图像与谷歌的照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从相机或画廊与使用谷歌应用程序的照片,让不幸的是照片裁剪图像已经经过作物停止的问题,并使用内建的意图裁剪功能保存photo.I'm。

下面是我的code。

 私人无效cropCapturedImage(URI picUri){
    尝试{
        //调用标准作物动作意图(用户设备可能不支持它)
        cropIntent =新意图(com.android.camera.action.CROP);
        //显示图像类型和URI
        cropIntent.setDataAndType(picUri,图像/ *);
        //设置裁剪性能
        cropIntent.putExtra(裁剪,真);
        //注明所需作物方面
        cropIntent.putExtra(aspectX,1);
        cropIntent.putExtra(aspectY,1);
        //指示调整图片大小输出X和Y
        cropIntent.putExtra(outputX,256);
        cropIntent.putExtra(outputY,256);
        //检索返回数据
        cropIntent.putExtra(回归数据,真正的);        //启动活动 - 我们处理返回的的onActivityResult
        startActivityForResult(cropIntent,PIC_CROP);
    }赶上(ActivityNotFoundException anfe){
        //显示错误信息
        Log.e(ERR_FAIL_TO_CROP,失败作物,因为:+ anfe.getMessage()的toString());    }赶上(例外富){
        Log.e(ERR_FAIL_TO_CROP,失败裁剪:( - + foo.getMessage()的toString());    }
}
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
    super.onActivityResult(要求code,结果code,数据);    如果(结果code == RESULT_OK){
        //用户从使用照相机拍摄图像返回
        如果(要求code == PIC_CROP){
            //获取返回的数据
            尝试{
                捆绑额外= data.getExtras();
                //获取裁剪位图
                位图thePic = extras.getParcelable(数据);                //显示返回裁剪图像
                profileImage.setImageBitmap(thePic);
                profileImage.setDrawingCacheEnabled(真);
            }赶上(例外五){                Log.e(DBG_SAVE_CROP_ERROR,错误信息:+ e.getMessage()的toString());            }
        }
    }    否则,如果(结果code == RESULT_CANCELED){
        Toast.makeText(SignupActivity.this!操作已取消,Toast.LENGTH_SHORT).show();
    }
}


解决方案

是否有可能从裁剪照片谷歌的图像?我创建该应用程序允许用户选择图像,然后裁剪。当选择一个图像的用户被要求选择一个应用程序来完成选择和剪裁图像从相册作品完美,但选择一个从谷歌图片,没有错误崩溃的动作(相册,谷歌照片等)。喜欢BBM和WhatsApp的应用程序允许从照片裁剪上传个人资料图片。我想要做类似的事情。

这里是我的code:

 意向意图=新的Intent();
intent.setType(图像/ *);
intent.setAction(Intent.ACTION_GET_CONTENT);
意图I =新意图(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(ⅰ,PICK_FROM_FILE);@覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
    开关(要求code){
    案例PICK_FROM_FILE:
    mImageCaptureUri = data.getData();
    意向意图=新意图(com.android.camera.action.CROP);
    intent.setType(图像/ *);    清单< ResolveInfo> 。名单= getPackageManager()queryIntentActivities(意向,0);    INT大小=则为list.size();    如果(大小== 0){
        Toast.makeText(这一点,找不到图像裁剪的应用程序,Toast.LENGTH_SHORT).show();
        返回;
    }其他{
        intent.setData(mImageCaptureUri);
        intent.putExtra(outputX,650);
        intent.putExtra(outputY,650);
        intent.putExtra(aspectX,1);
        intent.putExtra(aspectY,1);
        intent.putExtra(规模化,真正的);
        intent.putExtra(回归数据,真正的);
        意图I =新意图(意向);
        ResolveInfo解析度= list.get(0);
        i.setComponent(新组件名(res.activityInfo.packageName,res.activityInfo.name));
        startActivityForResult(ⅰ,CROP_FROM_CAMERA);
    }
    打破;
    案例CROP_FROM_CAMERA:
      捆绑额外= data.getExtras();
      如果(临时演员!= NULL){
         位图照片= extras.getParcelable(数据);
         mImageView.setImageBitmap(照片);
         mImageView.setBackgroundColor(0);
      }
      文件f =新的文件(mImageCaptureUri.getPath());
      如果(f.exists())f.delete();
      打破;
    }
}

P.S:这是奇巧及更高版本(API19)

when I crop Image from camera or gallery with using google photos app, getting Unfortunately Photos has stopped problem after crop and save photo.I'm using builtin Intent crop function.

Here is my code.

private void cropCapturedImage(Uri picUri) {


    try {
        //call the standard crop action intent (the user device may not support it)
        cropIntent = new Intent("com.android.camera.action.CROP");
        //indicate image type and Uri
        cropIntent.setDataAndType(picUri, "image/*");
        //set crop properties
        cropIntent.putExtra("crop", "true");
        //indicate aspect of desired crop
        cropIntent.putExtra("aspectX", 1);
        cropIntent.putExtra("aspectY", 1);
        //indicate output X and Y for resize picture
        cropIntent.putExtra("outputX", 256);
        cropIntent.putExtra("outputY", 256);
        //retrieve data on return
        cropIntent.putExtra("return-data", true);

        //start the activity - we handle returning in onActivityResult
        startActivityForResult(cropIntent, PIC_CROP);
    } catch (ActivityNotFoundException anfe) {
        //display an error message
        Log.e(ERR_FAIL_TO_CROP, "Fail to Crop because: " + anfe.getMessage().toString());

    } catch (Exception foo) {
        Log.e(ERR_FAIL_TO_CROP, "Fail to crop :( -- " + foo.getMessage().toString());

    }
}


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

    if (resultCode == RESULT_OK) {
        //user is returning from capturing an image using the camera
        if (requestCode == PIC_CROP) {
            //get the returned data
            try {
                Bundle extras = data.getExtras();
                //get the cropped bitmap
                Bitmap thePic = extras.getParcelable("data");

                //display the returned cropped image
                profileImage.setImageBitmap(thePic);
                profileImage.setDrawingCacheEnabled(true);


            } catch (Exception e) {

                Log.e(DBG_SAVE_CROP_ERROR, "Error message: " + e.getMessage().toString());

            }
        }
    }

    else if (resultCode == RESULT_CANCELED) {
        Toast.makeText(SignupActivity.this, "Action cancelled!", Toast.LENGTH_SHORT).show();
    }
}

解决方案

Is it possible to crop an image from Google Photos? The app I'm creating allows the user to select an image and crop it. When selecting an image the user is asked to select an app to complete the action(Album, Google Photos, etc) Selecting and cropping an image from album works perfectly, but selecting one from Google Photos crashes with no error. Apps like BBM and WhatsApp allows cropping from Photos to upload a profile picture. I want to do something similar.

Here's my Code:

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
Intent i = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, PICK_FROM_FILE);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case PICK_FROM_FILE:
    mImageCaptureUri = data.getData();
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setType("image/*");

    List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent, 0);

    int size = list.size();

    if (size == 0) {
        Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();
        return;
    } else {
        intent.setData(mImageCaptureUri);
        intent.putExtra("outputX", 650);
        intent.putExtra("outputY", 650);
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("scale", true);
        intent.putExtra("return-data", true);
        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);
    }
    break;
    case CROP_FROM_CAMERA:
      Bundle extras = data.getExtras();
      if (extras != null) {
         Bitmap photo = extras.getParcelable("data");
         mImageView.setImageBitmap(photo);
         mImageView.setBackgroundColor(0);
      }
      File f = new File(mImageCaptureUri.getPath());
      if (f.exists()) f.delete();
      break;
    }
}

P.S: This is for KitKat and Higher(API19)

这篇关于裁剪图像与谷歌的照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆