Intent.ACTION_GET_CONTENT用法

来源:互联网 发布:网络诈骗报警能追回吗 编辑:程序博客网 时间:2024/06/11 02:52

先对ACTION_GET_CONTENT做一下说明:
ACTION_GET_CONTENT:允许用户选择特殊种类的数据,并返回(特殊种类的数据:照一张相片或录一段音)

剪切图片

 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", true);        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile()));        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());        intent.putExtra("noFaceDetection",!faceDetection); // lol, negative boolean noFaceDetectionstartActivityForResult(intent, PHOTO_PICKED);
回调方法

   @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        String action = data.getAction();        //如果 intent.putExtra("return-data", true);则可以得到返回的Bitmap数据        final Bundle extras = data.getExtras();        Bitmap bitmap = extras.getParcelable("data");    }
如果传入在Intent中传入的参数MediaStore.EXTRA_OUTPUT的类型是File则,action 是Uri地址,如:

Intent { act=content://media/external/images/media/1843 (has extras) }
 如果传入的是Uri类型,则action是file地址,如:

Intent { act=file:///mnt/sdcard/tempPhoto.jpg (has extras) }

注意:1、以上代码是在2.3.3版本完成测试的。

            2、2.1版本之前 MediaStore.EXTRA_OUTPUT,该属性可以指定一个Uir,系统会将文件地址映射为该Uri地址,但在之后版本不支持该属性。