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

时间:2012-09-05 06:44:13

标签: android image android-intent crop android-image

我已经阅读了很多关于此的问题,但我仍然无法使用此代码......也许任何人都可以对我的代码进行核心...我想使用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();

但是当我运行代码时,没有任何内容...... T.T 任何人都可以帮助我吗?

4 个答案:

答案 0 :(得分:5)

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

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 {

        }

    }

和这样的activityResult

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

也许它有用:D

答案 1 :(得分:2)

使用此意图非常危险。它甚至不是文档的一部分。 Here's更多解释为什么它是一个冒险的事情。 我建议使用第三方库进行裁剪,例如this one

答案 2 :(得分:0)

然后启动活动,使用此

startActivityForResult(intent, 1);  

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

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

答案 3 :(得分:0)

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