Android系统。从图库裁剪图像

时间:2016-01-07 20:52:53

标签: java android image imageview crop

在我的应用程序中,我正在尝试裁剪图像。我发现一些代码可以在拍摄照片时拍摄照片,但是我无法从照片库裁剪照片,原因是:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=0, data=null} to activity {....MainActivity}: java.lang.NullPointerException "

在这一行:Bundle extras = data.getExtras();

这是myActivity的代码:

public class MainActivity extends Activity {
ImageView imVCature_pic;
Button btnCapture;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeControls();
}

private void initializeControls() {
imVCature_pic=(ImageView)findViewById(R.id.imVCature_pic);
btnCapture=(Button)findViewById(R.id.btnCapture);
Button buttonGallery = (Button) findViewById(R.id.btn_select_gallery);
btnCapture.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        /*create instance of File with name img.jpg*/
        File file = new File(Environment.getExternalStorageDirectory() + File.separator + "img.jpg");
        /*put uri as extra in intent object*/
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
        /*start activity for result pass intent as argument and request code */
        startActivityForResult(intent, 1);
    }
});

buttonGallery.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {


        Intent intent2 = new Intent();

        intent2.setType("image/*");
        intent2.setAction(Intent.ACTION_GET_CONTENT);
        File file2 = new File(Environment.getExternalStorageDirectory() + File.separator + "img2.jpg");
        /*put uri as extra in intent object*/
        intent2.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file2));
        /*start activity for result pass intent as argument and request code */
        startActivityForResult(intent2, 3);
    }} );
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent     data) {
    super.onActivityResult(requestCode, resultCode, data);
    //if request code is same we pass as argument in startActivityForResult
    if(requestCode==1){
        //create instance of File with same name we created before to get     image from storage
        File file = new     File(Environment.getExternalStorageDirectory()+File.separator + "img.jpg");
        //Crop the captured image using an other intent
        try {
            /*the user's device may not support cropping*/
            cropCapturedImage(Uri.fromFile(file));
        }
        catch(ActivityNotFoundException aNFE){
            //display an error message if user device doesn't support
            String errorMessage = "Sorry - your device doesn't support the crop action!";
            Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
        toast.show();
    }
}
if(requestCode==2){
    //Create an instance of bundle and get the returned data
    Bundle extras = data.getExtras();
    //get the cropped bitmap from extras
    Bitmap thePic = extras.getParcelable("data");
    //set image bitmap to image view
    imVCature_pic.setImageBitmap(thePic);
}
if(requestCode==3){
    //create instance of File with same name we created before to get image from storage
    File file = new File(Environment.getExternalStorageDirectory()+File.separator + "img.jpg");
    //Crop the captured image using an other intent
    try {
        /*the user's device may not support cropping*/
        cropCapturedImage(Uri.fromFile(file));
    }
        catch(ActivityNotFoundException aNFE){
            //display an error message if user device doesn't support
            String errorMessage = "Sorry - your device doesn't support the crop action!";
            Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
            toast.show();
        }
    }
}
    //create helping method cropCapturedImage(Uri picUri)
    public void cropCapturedImage(Uri picUri){
    //call the standard crop action intent
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri of image
cropIntent.setDataAndType(picUri, "image/*");
//set crop properties
cropIntent.putExtra("crop", "true");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 256);
cropIntent.putExtra("aspectY", 256);
//indicate output X and Y
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, 2);
}

哪里出错?

0 个答案:

没有答案