android 中用Intent提取缩略图和原始图像

提取图像的Thumbnail
  1)
  启动Intent
  Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
  intent.setType("image/*");
  intent.putExtra("return-data", true);
  startActivityForResult(intent, REQUEST_CODE_PHOTO_PICKED);
  

在 onActivityResult 中
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (resultCode != RESULT_OK) {
  return;
  }
  Bitmap bitmap = null;
  ContentResolver resolver = getContentResolver();
  if (requestCode == REQUEST_CODE_PHOTO_PICKED) {
  final Bundle extras = data.getExtras();
  if (extras != null)
  bitmap = extras.getParcelable("data");
  }
  }
  此方法返回的data 必须小于100k
  

  2)启动Intent
  Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
  intent.setType("image/*");
  startActivityForResult(intent, REQUEST_CODE_PHOTO_PICKED);
  在 onActivityResult 中
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (resultCode != RESULT_OK) {
  return;
  }
  Bitmap bitmap = null;
  ContentResolver resolver = getContentResolver();
  if (requestCode == REQUEST_CODE_PHOTO_PICKED) {
  try {
  Uri originalUri = data.getData();
  Uri thumb = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, originalUri.getLastPathSegment());
  bitmap = MediaStore.Images.Media.getBitmap(resolver, thumb);
  } catch (IOException e) {
  }
  }
  }

你可能感兴趣的:(android)