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; @Override protected 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{ @Override public 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; } } } @Override protected 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>