步驟如下:
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"
android:icon="@drawable/ic_launcher_gallery">
...
...
沒有留言:
張貼留言