从相册选相片并裁剪

我想实现一个功能,就是用户从摄像头拍个照然后裁剪后把图片显示到屏幕,或者从相册选一张裁剪后显示到屏幕。以下是代码,但是在从相册选了图片后,没有弹出裁剪的界面,直接返回主界面了。。
请大神帮看下。这是一本书的代码,没想到不能运行。是4.0.4的安卓系统。
package com.example.pstar.photodemo;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class MainActivity extends Activity {

public static final int TAKE_PHOTO = 1;

public static final int CROP_PHOTO = 2;

private Button takePhoto; //点这个按钮拍照并裁剪

private Button chooseFromAlbum; //点这个按钮是从相册选一张相片并裁剪

private ImageView picture;

private Uri imageUri;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    takePhoto = (Button) findViewById(R.id.take_photo);
    chooseFromAlbum = (Button) findViewById(R.id.choose_from_album);
    picture = (ImageView) findViewById(R.id.picture);
    takePhoto.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            File outputImage = new File(Environment.getExternalStorageDirectory(),
                    "output_image.jpg");
            try {
                if (outputImage.exists()) {
                    outputImage.delete();
                }
                outputImage.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            imageUri = Uri.fromFile(outputImage);
            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            startActivityForResult(intent, TAKE_PHOTO);
        }
    });
    chooseFromAlbum.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            File outputImage = new File(Environment.getExternalStorageDirectory(),
                    "output_image.jpg");
            try {
                if (outputImage.exists()) {
                    outputImage.delete();
                }
                outputImage.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            imageUri = Uri.fromFile(outputImage);
            Intent intent = new Intent("android.intent.action.GET_CONTENT");
            intent.setType("image/*");
            intent.putExtra("crop", true);
            intent.putExtra("scale", true);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            startActivityForResult(intent, CROP_PHOTO);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case TAKE_PHOTO:
            if (resultCode == RESULT_OK) {
                Intent intent = new Intent("com.android.camera.action.CROP");
                intent.setDataAndType(imageUri, "image/*");
                intent.putExtra("scale", true);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(intent, CROP_PHOTO);
            }
            break;
        case CROP_PHOTO:
            if (resultCode == RESULT_OK) {
                try {
                    Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver()
                            .openInputStream(imageUri));
                    picture.setImageBitmap(bitmap);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
            break;
        default:
            break;
    }
}

}

3 个回答

1
采纳
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(imageUri, "image/*");
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CROP_PHOTO);

这段代码的作用是跳转到剪裁界面
而你在选择照片后进的是这个分支

case CROP_PHOTO:
    if (resultCode == RESULT_OK) {
        try {
            Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver()
            .openInputStream(imageUri));
            picture.setImageBitmap(bitmap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    break;

当然不会跳转到剪裁界面

修改方法:

chooseFromAlbumOnClickListener里的startActivityForResult(intent, CROP_PHOTO)改成startActivityForResult(intent, TAKE_PHOTO)

**********************更新*********************
先说原因:

之所以无法加载图片,是因为这张图片没有生成。题主可以查看SD卡路径下的output_image.jpg,会看到它是一个0字节文件。

解决方法:

把`Intent intent = new Intent("android.intent.action.GET_CONTENT")`改成`Intent intent = new Intent("android.intent.action.PICK")`
if (resultCode == RESULT_OK) {
    if (data != null) {
        imageUri = data.getData();
    }
    Intent intent = new Intent("com.android.camera.action.CROP");
    ...
}

说明:

至于"android.intent.action.GET_CONTENT""android.intent.action.PICK"的区别是什么,题主有兴趣的话可以搜索一下或者查看官方文档,没兴趣的话可以暂时不用管。只要记住,如果要拿本地已经存在的资源(image, vedio等)时,一律用"android.intent.action.PICK"

http://developer.android.com/reference/android/content/Intent.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值