在android eclipse中裁剪图像

时间:2012-09-28 08:24:57

标签: android image

我有一个拍照的功能。用户可以从相机或图库中拍摄照片。 如果用户选择图片,该图片将显示在另一个活动中。 在其他活动中,用户可以编辑此图片。 在这种情况下,我想制作一个裁剪图像的功能。  但是当它运行时,我选择裁剪时,没有任何反应。你能给我一个解决方案吗?这是我的代码

package shoop3.android.edu;

import java.io.File;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;

import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ResolveInfo;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.MediaStore;
import android.view.View;

import android.net.Uri;

import android.os.Bundle;
import android.os.Environment;

import android.widget.Button;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.widget.ImageView;

public class editorActivity extends Activity {
    private Uri mImageCaptureUri;
    private ImageView mImageView;


    private static final int CROP= 1;
    private static final int HASIL_CROP = 2;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.editor);

        ImageView imageeditor = (ImageView)findViewById(R.id.ImageViewEditor);
        Intent hasil = getIntent();

        String path = hasil.getStringExtra("pathimage");
        mImageCaptureUri = hasil.getData();
        //Bitmap b = (Bitmap) getIntent().getParcelableExtra("mImageView");
        imageeditor.setImageBitmap(BitmapFactory.decodeFile(path));

        final String [] items           = new String [] {"CROP"};               
        ArrayAdapter<String> adapter    = new ArrayAdapter<String> (this, android.R.layout.select_dialog_item,items);
        AlertDialog.Builder builder     = new AlertDialog.Builder(this);

        builder.setTitle("Image Editor");
        builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
            public void onClick( DialogInterface dialog, int item ) { 

                if(item == 0){
                Intent i = getIntent();
                mImageCaptureUri = i.getData();
                startActivityForResult(i, CROP);
                }


                }

        } );

        final AlertDialog dialog = builder.create();

        //Button button     = (Button) findViewById(R.id.btn_crop);
        mImageView      = (ImageView) findViewById(R.id.ImageViewEditor);
        mImageView.setOnClickListener(new View.OnClickListener() {  
            @Override
            public void onClick(View v) {
                dialog.show();
            }
        });
    }

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

        switch (requestCode) {


            case CROP: 
                mImageCaptureUri = data.getData();

                doCrop();

                break;          

            case HASIL_CROP:            
                Bundle extras = data.getExtras();

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

                    mImageView.setImageBitmap(photo);
                }

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

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

                break;

        }
    }

    private void doCrop() {

        try{
            Intent cropintent = new Intent("com.android.action.CROP");
            cropintent.setDataAndType(mImageCaptureUri, "/image/*");
            cropintent.putExtra("crop", true);
            cropintent.putExtra("aspectX", 1);
            cropintent.putExtra("aspectY", 1);
            cropintent.putExtra("outputX", 256);
            cropintent.putExtra("outputY", 256);
            cropintent.putExtra("return-data", true);
            startActivityForResult(cropintent, HASIL_CROP);
        }catch(ActivityNotFoundException e){
            String errorMessage = "Your device not Support";
            Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show();
        }

    }}

1 个答案:

答案 0 :(得分:-1)

您在此行中使用的裁剪方法:

        Intent cropintent = new Intent("com.android.action.CROP");

并非所有Android设备都支持。我有同样的问题,我现在正在为自己解决这个问题。

修改 你的问题可能只是忘记了相机:

        Intent cropintent = new Intent("com.android.camera.action.CROP");