将裁剪的图像保存到图库中

时间:2013-01-18 07:04:23

标签: android image android-gallery

  

可能重复:
  I want to save only the cropped image into the gallery. but it saves the original full image. how to save only the cropped image?

我想将裁剪后的图像保存到图库中,但下面的代码只存储了原始图片怎么办?

 package com.example.imagecropp;

import android.app.Activity;

import android.content.ActivityNotFoundException;

import android.content.Intent;

import android.graphics.Bitmap;

import android.net.Uri;

import android.os.Bundle;

import android.provider.MediaStore;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.Toast;

public class ImageCroppMainActivity extends Activity implements OnClickListener {

    final int CAMERA_CAPTURE = 1;

    final int PIC_CROP = 2;

    private Uri picUri;

    final int PICK_IMAGE = 3;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_image_cropp_main);

        Button captureBtn = (Button) findViewById(R.id.capture_btn);

        captureBtn.setOnClickListener(this);

        Button browseBtn = (Button) findViewById(R.id.browse_btn);

        browseBtn.setOnClickListener(this);

    }

    public void onClick(View v) {

        if (v.getId() == R.id.capture_btn) {

            try {

                Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                startActivityForResult(captureIntent, CAMERA_CAPTURE);

            } catch (ActivityNotFoundException anfe) {

                String errorMessage = "Whoops - your device doesn't support capturing images!";

                Toast toast = Toast.makeText(this, errorMessage,Toast.LENGTH_SHORT);

                toast.show();

            }

        } 

        else if (v.getId() == R.id.browse_btn) {

            Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(intent, PICK_IMAGE);

        }

    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == RESULT_OK) {

            if (requestCode == CAMERA_CAPTURE) {

                picUri = data.getData();

                performCrop();

            }

            else if (requestCode == PIC_CROP) {

                Bundle extras = data.getExtras();

                Bitmap thePic = extras.getParcelable("data");

                ImageView picView = (ImageView) findViewById(R.id.picture);

                MediaStore.Images.Media.insertImage(getContentResolver(),thePic, "", "");

                picView.setImageBitmap(thePic);

            } else if (requestCode == PICK_IMAGE) {

                picUri = data.getData();

                performCrop();

            }

        }

    }

    private void performCrop() {

        try {

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

            cropIntent.setDataAndType(picUri, "image/*");

            cropIntent.putExtra("crop", "false");

            cropIntent.putExtra("aspectX", 1);

            cropIntent.putExtra("aspectY", 1);

            cropIntent.putExtra("outputX", 256);

            cropIntent.putExtra("outputY", 256);

            cropIntent.putExtra("return-data", true);

            startActivityForResult(cropIntent, PIC_CROP);

        }

        catch (ActivityNotFoundException anfe) {

            String errorMessage = "Whoops - your device doesn't support the crop action!";

            Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);

            toast.show();

        }

    }

}

0 个答案:

没有答案