在 android 上使用 com.android.camera.action.CROP 裁剪保存的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12275842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:17:26  来源:igfitidea点击:

Crop saved Image using com.android.camera.action.CROP on android

androidimageandroid-intentcropandroid-image

提问by adi.zean

I have reads many question about this, but I still failed using this code... maybe anyone can corect my code... I want to crop an image from file that i know the location using com.android.camera.action.CROP like this...

我已经阅读了很多关于此的问题,但我仍然无法使用此代码......也许任何人都可以纠正我的代码......我想从我知道使用 com.android.camera.action.CROP 的位置的文件中裁剪图像像这样...

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

    intent.setData(mImageCaptureUri); 
    intent.putExtra("crop", true);
    intent.putExtra("outputX", 200);
    intent.putExtra("outputY", 200);
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    intent.putExtra("scale", true);
    intent.putExtra("return-data", true);

    Bundle extras = intent.getExtras();

    if (extras != null) {               
        Bitmap photo = extras.getParcelable("intent");

        tampilan.setImageBitmap(photo);
    }

    File f = new File(mImageCaptureUri.getPath());            

    if (f.exists()) f.delete();

But when i run the code, nothing hapend... T.T can anyone help me??

但是当我运行代码时,什么也没有发生...... TT 任何人都可以帮助我吗??

回答by adi.zean

I had modify my code and its works successfull, this is my code..

我修改了我的代码并且它的工作成功了,这是我的代码..

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(selectImageUri);        
        intent.putExtra("outputX", 300);
        intent.putExtra("outputY", 300);
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("scale", true);
        intent.putExtra("return-data", true);
        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_RESULT);
        } else {

        }

    }

and the activityResult like this

和这样的活动结果

if (resultCode == RESULT_OK){
        switch (requestCode){
        case SELECT_PICTURE :
            selectImageUri = data.getData();
            doCrop();
        break;
        case CROP_RESULT :
            Bundle extras = data.getExtras();

            if (extras != null) {               
                bmp = extras.getParcelable("data");
                temporary.setBitmap(bmp);

            }
            File f = new File(selectImageUri.getPath());            

            if (f.exists()) f.delete();
            Intent inten3 = new Intent(this, tabActivity.class);
            startActivity(inten3);
        break;
        case CAMERA_IMAGE :
            doCrop();
        break;
        }
    }

maybe its useful :D

也许它有用:D

回答by android developer

Using this intent is very risky. It's not even a part of the documentation. Here'ssome more explanation about why it's a risky thing. I suggest using a third party library for cropping, like this one.

使用这个意图是非常危险的。它甚至不是文档的一部分。这里有一些关于为什么这是一件有风险的事情的更多解释。我建议使用第三方库进行裁剪,比如这个

回答by Augusto Favretto

    try {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(mPhotoUri, "image/*");
        intent.putExtra("crop", "true");
        Integer altura = documento.getAltura();
        Integer largura = documento.getLargura();
        if (altura != null && largura != null) {
            intent.putExtra("aspectY", altura);
            intent.putExtra("aspectX", largura);
        }
        File file = imagemProcessor.getNewFile();
        mCropUri = Uri.fromFile(file);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, mCropUri);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        startActivityForResult(intent, REQUEST_IMAGE_CROP);
    } catch (ActivityNotFoundException e) {
        Context context = getBaseContext();
        Toast toast = Toast.makeText(context, "Your device doesn't support the crop action!", Toast.LENGTH_SHORT);
        toast.show();
    } catch (IOException e) {
        Context context = getBaseContext();
        Toast toast = Toast.makeText(context, "Fail to create file!", Toast.LENGTH_SHORT);
        toast.show();
    }

回答by Shalini

Then start the activity, use this

然后开始活动,使用这个

startActivityForResult(intent, 1);  

then use the following to get croped image

然后使用以下内容获取裁剪图像

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK)return;

        switch (requestCode) {

            case 1:         
                Bundle extras = data.getExtras();
                if (extras != null) {               
                    photo = extras.getParcelable("data");               


                        tampilan.setBitmap(photo);
break;
}
}
}