程序分析:
主要分两个部分
(1)先拍照,然后剪切,最后显示出来。 (2)从相册中选择一张照片进行剪切然后显示出来
代码参照《第一行代码》,书中的代码运行时(2)出现了问题,无法剪切图片且无法加载图片
MainActivity.java代码如下:
1 package com.example.lynn.choosepictest; 2 3 import android.content.Intent; 4 import android.graphics.Bitmap; 5 import android.graphics.BitmapFactory; 6 import android.net.Uri; 7 import android.os.Environment; 8 import android.provider.MediaStore; 9 import android.support.v7.app.AppCompatActivity; 10 import android.os.Bundle; 11 import android.util.Log; 12 import android.view.View; 13 import android.widget.Button; 14 import android.widget.ImageView; 15 16 import java.io.File; 17 import java.io.FileNotFoundException; 18 19 public class MainActivity extends AppCompatActivity { 20 21 public static final int TAKE_PHOTO = 1; 22 public static final int CROP_PHOTO = 2; 23 private Button takePic; 24 private Button chooseBtn; 25 private ImageView picture; 26 private Uri imageUri; 27 28 @Override 29 protected void onCreate(Bundle savedInstanceState) { 30 super.onCreate(savedInstanceState); 31 setContentView(R.layout.activity_main); 32 takePic = (Button) findViewById(R.id.take_pic); 33 chooseBtn = (Button) findViewById(R.id.choose_from_album); 34 35 chooseBtn.setOnClickListener(new View.OnClickListener() { 36 @Override 37 public void onClick(View v) { 38 File outputImage = new File(Environment.getExternalStorageDirectory() 39 , "output_Image.jpg"); 40 try { 41 if (outputImage.exists()) { 42 outputImage.delete(); 43 } 44 outputImage.createNewFile(); 45 } catch (Exception e) { 46 e.printStackTrace(); 47 } 48 49 imageUri = Uri.fromFile(outputImage); 50 Intent intent = new Intent("android.intent.action.PICK"); 51 intent.setType("image/*"); 52 intent.putExtra("crop", true); 53 intent.putExtra("scale", true); 54 // intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri); 55 // 书中的这个代码没有运行,说明选中图片imageUri没有传过去,或者说就是空的! 56 startActivityForResult(intent, TAKE_PHOTO);//参数传TAKE_PHOTO 57 } 58 }); 59 60 takePic.setOnClickListener(new View.OnClickListener() { 61 @Override 62 public void onClick(View v) { 63 64 File outputImage = new File(Environment.getExternalStorageDirectory(), "tempImage.jpg"); 65 try { 66 if (outputImage.exists()) { 67 outputImage.delete(); 68 } 69 outputImage.createNewFile(); 70 } catch (Exception e) { 71 e.printStackTrace(); 72 } 73 imageUri = Uri.fromFile(outputImage); 74 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 75 intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 76 startActivityForResult(intent, TAKE_PHOTO);//启动相机程序 77 78 } 79 }); 80 } 81 82 @Override 83 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 84 85 switch (requestCode) { 86 //裁剪工作 87 case TAKE_PHOTO: 88 89 if (resultCode == RESULT_OK) { 90 if(data != null) { 91 imageUri = data.getData(); //此句是关键 在选中图片后imageUri并没有取到值,所以在这取值 92 } 93 Log.e("TAG","TAKE_PHOTO"); 94 Intent intent1 = new Intent("com.android.camera.action.CROP"); 95 intent1.setDataAndType(imageUri, "image/*"); 96 intent1.putExtra("scale", true); 97 intent1.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 98 startActivityForResult(intent1, CROP_PHOTO); 99 } 100 break; 101 102 103 //显示图片 104 case CROP_PHOTO: 105 if (resultCode == RESULT_OK) { 106 try { 107 Bitmap bitmap = BitmapFactory.decodeStream( 108 getContentResolver() 109 .openInputStream(imageUri)); 110 picture.setImageBitmap(bitmap); 111 } catch (FileNotFoundException e) { 112 e.printStackTrace(); 113 } 114 } 115 break; 116 default: 117 break; 118 } 119 } 120 }