search

Home  >  Q&A  >  body text

android - 安卓从相册选相片并裁剪

我想实现一个功能,就是用户从摄像头拍个照然后裁剪后把图片显示到屏幕,或者从相册选一张裁剪后显示到屏幕。以下是代码,但是在从相册选了图片后,没有弹出裁剪的界面,直接返回主界面了。。
请大神帮看下。这是一本书的代码,没想到不能运行。是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;
    }
}

}

PHPzPHPz2835 days ago651

reply all(4)I'll reply

  • 黄舟

    黄舟2017-04-17 13:09:51

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

    The function of this code is to jump to the cropping interface
    And this is the branch you entered after selecting the photo

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

    Of course it will not jump to the cropping interface

    Modification method:

    Change the chooseFromAlbum in OnClickListener of startActivityForResult(intent, CROP_PHOTO) to startActivityForResult(intent, TAKE_PHOTO)

    ************************UPDATE**********************
    Let’s talk about the reason first:

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

    Solution:

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

    Description:

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

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

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 13:09:51

    Debug with breakpoints to see if the code for cropping photos is running

    reply
    0
  • PHPz

    PHPz2017-04-17 13:09:51

    Haha What you are looking at is the first line of code~~~

    reply
    0
  • 阿神

    阿神2017-04-17 13:09:51

    Thanks for solving a big problem in the first line of code. I found it after searching online for a long time. It is really hidden deep! !

    reply
    0
  • Cancelreply