1

可能重复:
我只想将裁剪后的图像保存到图库中。但它保存了原始的完整图像。如何只保存裁剪的图像?

我想将裁剪后的图像保存到图库中,但以下代码仅存储原始图像怎么做?

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

        }

    }

}
4

0 回答 0