MainActivity如下:

package cn.testphotocrop;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.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;/** * Demo描述: * 1 从系统图片库中选取照片进行显示 * 2 从系统图片库中选取照片进行剪切然后显示 *  * 延伸阅读: * 自定义图片剪裁 * http://blog.csdn.net/liuhanhan512/article/details/8308525 */public class MainActivity extends Activity {    private Button mSelectButton;    private Button mCropButton;    private ImageView mImageView;    private final int SELECT_PHOTO=9527;    private final int CROP_PHOTO=7;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);init();}    private void init(){    mSelectButton=(Button) findViewById(R.id.selectorButton);    mSelectButton.setOnClickListener(new ClickListenerImpl());    mCropButton=(Button) findViewById(R.id.cropButton);    mCropButton.setOnClickListener(new ClickListenerImpl());    mImageView=(ImageView) findViewById(R.id.imageView);    }   private class ClickListenerImpl implements OnClickListener{@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.selectorButton:Intent intent_select = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);     startActivityForResult(intent_select, SELECT_PHOTO);  break;case R.id.cropButton:Intent intent_crop = getImageCropIntent();startActivityForResult(intent_crop, CROP_PHOTO);  break;default:break;}}   }   @Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (requestCode==SELECT_PHOTO) {try {Uri imageUri=data.getData();Bitmap bitmap=BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri), null, null);mImageView.setImageBitmap(bitmap);} catch (Exception e) {}}if (requestCode==CROP_PHOTO) {//注意://此处getParcelableExtra("data")中的"data"标签应该与//getImageCropIntent()方法中的 intent.putExtra("return-data", true)处//"return-data"里的"data"保持一致Bitmap bitmap = data.getParcelableExtra("data");       mImageView.setImageBitmap(bitmap);  }}private Intent getImageCropIntent() {Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);intent.setType("image/*");intent.putExtra("crop", "true");intent.putExtra("aspectX", 1);intent.putExtra("aspectY", 1);intent.putExtra("outputX", 80);intent.putExtra("outputY", 80);intent.putExtra("return-data", true);return intent;}  }


main.xml如下:

<RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent">    <Button        android:id="@+id/selectorButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="选择照片"        android:layout_centerHorizontal="true"        android:layout_marginTop="10dip"    />        <Button        android:id="@+id/cropButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="剪裁照片"        android:layout_centerHorizontal="true"        android:layout_marginTop="80dip"    />    <ImageView         android:id="@+id/imageView"        android:layout_below="@id/cropButton"        android:layout_width="fill_parent"        android:layout_height="fill_parent"     /></RelativeLayout>


更多相关文章

  1. 调用摄像头拍照并显示
  2. Android(安卓)获取系统自带相机的照片
  3. Android-FilePicker 选择照片
  4. Android(安卓)照片墙功能实现
  5. Android(安卓)拍照后显示照片
  6. Android录音,拍照,摄像
  7. Android(安卓)兼容Android(安卓)7拍摄照片/打开相册/选择照片/剪
  8. Android(安卓)知识要点整理(7)----拍照和录像
  9. Android实现上传拍下的照片到服务器

随机推荐

  1. Android知识体系总结之Android部分Activi
  2. android加载框效果《IT蓝豹》
  3. listview原生态属性
  4. Android(安卓)Gradle Plugin指南(一)——简
  5. Android(安卓)SDK Android(安卓)NDK Andr
  6. Android(安卓)Fresco属性大全,中文说明
  7. Android(安卓)Gradle Plugin指南(一)——简
  8. Android剖析和运行机制
  9. Android开发从入门到精通
  10. 通过ant脚本,编译打包android工程