你好,游客 登录 注册 搜索
背景:
阅读新闻

如何使用Android MediaStore裁剪大图片

[日期:2012-11-11] 来源:CSDN  作者:floodingfire [字体: ]

方法1:如果你将return-data设置为“true”,你将会获得一个与内部数据关联的Action,并且bitmap以此方式返回:(Bitmap)extras.getParcelable("data")。注意:如果你最终要获取的图片非常大,那么此方法会给你带来麻烦,所以你要控制outputX和outputY保持在较小的尺寸。鉴于此原因,在我的代码中没有使用此方法((Bitmap)extras.getParcelable("data"))。

下面是CropImage.java的源码片段:

// Return the cropped image directly or save it to the specified URI.
Bundle myExtras = getIntent().getExtras();
if (myExtras != null && (myExtras.getParcelable("data") != null || myExtras.getBoolean("return-data")))
{
    Bundle extras = new Bundle();
    extras.putParcelable("data", croppedImage);
    setResult(RESULT_OK,(new Intent()).setAction("inline-data").putExtras(extras));
    finish();
}

方法2: 如果你将return-data设置为“false”,那么在onActivityResult的Intent数据中你将不会接收到任何Bitmap,相反,你需要将MediaStore.EXTRA_OUTPUT关联到一个Uri,此Uri是用来存放Bitmap的。

但是还有一些条件,首先你需要有一个短暂的与此Uri相关联的文件地址,当然这不是个大问题(除非是那些没有sdcard的设备)。

下面是CropImage.java关于操作Uri的源码片段:

if (mSaveUri != null) {
    OutputStream outputStream = null;
    try {
        outputStream = mContentResolver.openOutputStream(mSaveUri);
        if (outputStream != null) {
            croppedImage.compress(mOutputFormat, 75, outputStream);
        }
    } catch (IOException ex) {
        // TODO: report error to caller
        Log.e(TAG, "Cannot open file: " + mSaveUri, ex);
    } finally {
        Util.closeSilently(outputStream);
    }
    Bundle extras = new Bundle();
    setResult(RESULT_OK, new Intent(mSaveUri.toString()).putExtras(extras));
}

代码示例:

我已经附上了一些代码示例,应该可以让你测试多种配置。请让我知道它对你是否有用。

代码下载: MediaStoreTest

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是www.linuxidc.com

具体下载目录在 /2012年资料/11月/11日/如何使用Android MediaStore裁剪大图片

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 thiz = this;
 setContentView(R.layout.main);
 mBtn = (Button) findViewById(R.id.btnLaunch);
 photo = (ImageView) findViewById(R.id.imgPhoto);
 mBtn.setOnClickListener(new OnClickListener() {

  public void onClick(View v) {
   try {
    // Launch picker to choose photo for selected contact
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
    intent.setType("image/*");
    intent.putExtra("crop", "true");
    intent.putExtra("aspectX", aspectX);
    intent.putExtra("aspectY", aspectY);
    intent.putExtra("outputX", outputX);
    intent.putExtra("outputY", outputY);
    intent.putExtra("scale", scale);
    intent.putExtra("return-data", return_data);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri());
    intent.putExtra("outputFormat",
      Bitmap.CompressFormat.JPEG.toString()); <span style="color: rgb(72, 70, 90); line-height: normal; font-family: monospace; font-size: 11px; background-color: rgb(239, 239, 239);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// lol, negative boolean noFaceDetection</span> intent.putExtra("noFaceDetection", !faceDetection);
    if (circleCrop) {
     intent.putExtra("circleCrop", true);
    }

    startActivityForResult(intent, PHOTO_PICKED);
   } catch (ActivityNotFoundException e) {
    Toast.makeText(thiz, R.string.photoPickerNotFoundText,
      Toast.LENGTH_LONG).show();
   }
  }
 });

}

private Uri getTempUri() {
 return Uri.fromFile(getTempFile());
}

private File getTempFile() {
 if (isSDCARDMounted()) {

  File f = new File(Environment.getExternalStorageDirectory(),
    TEMP_PHOTO_FILE);
  try {
   f.createNewFile();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   Toast.makeText(thiz, R.string.fileIOIssue, Toast.LENGTH_LONG)
     .show();
  }
  return f;
 } else {
  return null;
 }
}

private boolean isSDCARDMounted() {
 String status = Environment.getExternalStorageState();

 if (status.equals(Environment.MEDIA_MOUNTED))
  return true;
 return false;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 super.onActivityResult(requestCode, resultCode, data);

 switch (requestCode) {
 case PHOTO_PICKED:
  if (resultCode == RESULT_OK) {
   if (data == null) {
    Log.w(TAG, "Null data, but RESULT_OK, from image picker!");
    Toast t = Toast.makeText(this, R.string.no_photo_picked,
      Toast.LENGTH_SHORT);
    t.show();
    return;
   }

   final Bundle extras = data.getExtras();
   if (extras != null) {
    File tempFile = getTempFile();
    // new logic to get the photo from a URI
    if (data.getAction() != null) {
     processPhotoUpdate(tempFile);
    }
   }
  }
  break;
 }
}

附录:My comments
Thank you so much! The tutorial is great! Actually the secret of cropping photos on Android is using Uri if the photo is in large size and using Bitmap if you want but make sure that the bitmap is not too big.(You can use it for cropping avatar or other requirements with a limited size of the photo. Different phones have different limits. Normally if you want to use a bitmap, the size shouldn't be bigger than 300. Otherwise the Uri is suggested.)

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款