从相册的列表中选择头象,并将其裁剪
步骤如下:
1、触发一个单击事件,在单击时间中发送一个 Intent
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, 96);
intent.putExtra(outputY, 96);
try {
intent.putExtra(return-data, true);//将是否有结果返回的标志位改为true
startActivityForResult(intent, PHOTO_PICKED_WITH_DATA);
} catch (ActivityNotFoundException e) {
new AlertDialog.Builder(EditContactActivity.this)
.setTitle(R.string.errorDialogTitle)
.setMessage(R.string.photoPickerNotFoundText)
.setPositiveButton(android.R.string.ok, null)
.show();
}
2、重写onActivityResult方法:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) {
return;
}
switch (requestCode) {
case PHOTO_PICKED_WITH_DATA: {
final Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable(data);
mPhoto = photo;
mPhotoChanged = true;
mPhotoImageView.setImageBitmap(photo);
setPhotoPresent(true);
}
break;
}
}
}
3、显示图片列表的Activity需要满足:
A. 将显示的结果保存
Intent result = new Intent(null, img.fullSizeImageUri());
if (myExtras != null && myExtras.getString(return-data) != null) {
Bitmap bitmap = img.fullSizeBitmap(1000);
if (bitmap != null)
result.putExtra(data, bitmap);
}
setResult(RESULT_OK, result);
finish();
B、设置android:name=android.intent.action.GET_CONTENT
<activity android:name=ImageGallery2 android:label=@string/gallery_label
android:configChanges=orientation|keyboardHidden
android:icon=@drawable/ic_launcher_gallery>
...
<intent-filter>
<action android:name=android.intent.action.GET_CONTENT />
<category android:name=android.intent.category.OPENABLE />
<data android:mimeType=vnd.android.cursor.dir/image />
</intent-filter>
<intent-filter>
<action android:name=android.intent.action.GET_CONTENT />
<category android:name=android.intent.category.OPENABLE />
<category android:name=android.intent.category.DEFAULT />
<data android:mimeType=image/* />
<data android:mimeType=video/* />
</intent-filter>
...
</activity>
以上出自 Camera.java 源文件