码迷,mamicode.com
首页 > 其他好文 > 详细

每天进步一点----- 开启照相机,剪切相片

时间:2016-03-31 23:27:16      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

  1 package com.example.choosepictest;
  2 
  3 import java.io.File;
  4 import java.io.FileNotFoundException;
  5 import java.io.IOException;
  6 
  7 import android.app.Activity;
  8 import android.content.Intent;
  9 import android.graphics.Bitmap;
 10 import android.graphics.BitmapFactory;
 11 import android.net.Uri;
 12 import android.os.Bundle;
 13 import android.os.Environment;
 14 import android.provider.MediaStore;
 15 import android.view.View;
 16 import android.view.View.OnClickListener;
 17 import android.widget.Button;
 18 import android.widget.ImageView;
 19 
 20 public class MainActivity extends Activity {
 21 
 22     public static final int TAKE_PHOTO = 1;
 23 
 24     public static final int CROP_PHOTO = 2;
 25 
 26     private Button takePhoto;
 27 
 28     private Button chooseFromAlbum;
 29 
 30     private ImageView picture;
 31 
 32     private Uri imageUri;
 33 
 34     @Override
 35     protected void onCreate(Bundle savedInstanceState) {
 36         super.onCreate(savedInstanceState);
 37         setContentView(R.layout.activity_main);
 38         takePhoto = (Button) findViewById(R.id.take_photo);
 39         chooseFromAlbum = (Button) findViewById(R.id.choose_from_album);
 40         picture = (ImageView) findViewById(R.id.picture);
 41         takePhoto.setOnClickListener(new OnClickListener() {
 42             @Override
 43             public void onClick(View v) {
 44                 File outputImage = new File(Environment.getExternalStorageDirectory(),
 45                         "output_image.jpg");
 46                 try {
 47                     if (outputImage.exists()) {
 48                         outputImage.delete();
 49                     }
 50                     outputImage.createNewFile();
 51                 } catch (IOException e) {
 52                     e.printStackTrace();
 53                 }
 54                 imageUri = Uri.fromFile(outputImage);
 55                 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
 56                 intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
 57                 startActivityForResult(intent, TAKE_PHOTO);
 58             }
 59         });
 60         chooseFromAlbum.setOnClickListener(new OnClickListener() {
 61             @Override
 62             public void onClick(View v) {
 63                 File outputImage = new File(Environment.getExternalStorageDirectory(),
 64                         "output_image.jpg");
 65                 try {
 66                     if (outputImage.exists()) {
 67                         outputImage.delete();
 68                     }
 69                     outputImage.createNewFile();
 70                 } catch (IOException e) {
 71                     e.printStackTrace();
 72                 }
 73                 imageUri = Uri.fromFile(outputImage);
 74                 Intent intent = new Intent("android.intent.action.GET_CONTENT");
 75                 intent.setType("image/*");
 76                 intent.putExtra("crop", true);
 77                 intent.putExtra("scale", true);
 78                 intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
 79                 startActivityForResult(intent, CROP_PHOTO);
 80             }
 81         });
 82     }
 83 
 84     @Override
 85     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 86         switch (requestCode) {
 87         case TAKE_PHOTO:
 88             if (resultCode == RESULT_OK) {
 89                 Intent intent = new Intent("com.android.camera.action.CROP");
 90                 intent.setDataAndType(imageUri, "image/*");
 91                 intent.putExtra("scale", true);
 92                 intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
 93                 startActivityForResult(intent, CROP_PHOTO);
 94             }
 95             break;
 96         case CROP_PHOTO:
 97             if (resultCode == RESULT_OK) {
 98                 try {
 99                     Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver()
100                             .openInputStream(imageUri));
101                     picture.setImageBitmap(bitmap);
102                 } catch (FileNotFoundException e) {
103                     e.printStackTrace();
104                 }
105             }
106             break;
107         default:
108             break;
109         }
110     }
111 
112 }

 

每天进步一点----- 开启照相机,剪切相片

标签:

原文地址:http://www.cnblogs.com/hjc-blog/p/5343179.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!