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

橙三吉。 提交于 2019-11-28 09:07:45

问题


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...

    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??


回答1:


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




回答2:


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




回答3:


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;
}
}
}



回答4:


    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();
    }


来源:https://stackoverflow.com/questions/12275842/crop-saved-image-using-com-android-camera-action-crop-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!