Intent.ACTION_GET_CONTENT用法

来源:互联网 发布:修复php intl.dll软件 编辑:程序博客网 时间:2024/06/04 20:41

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

剪切图片

[java] view plaincopy
  1.  Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);  
  2.         intent.setType("image/*");  
  3.         intent.putExtra("crop""true");  
  4.         intent.putExtra("aspectX", aspectX);  
  5.         intent.putExtra("aspectY", aspectY);  
  6.         intent.putExtra("outputX", outputX);      
  7.         intent.putExtra("outputY", outputY);  
  8.         intent.putExtra("scale", scale);  
  9. //      intent.putExtra("return-data", true);  
  10.         intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile()));  
  11.         intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());  
  12.         intent.putExtra("noFaceDetection",!faceDetection); // lol, negative boolean noFaceDetection  
  13.     startActivityForResult(intent, PHOTO_PICKED);  
回调方法

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

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

[java] view plaincopy
  1. Intent { act=file:///mnt/sdcard/tempPhoto.jpg (has extras) }  

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

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

0 0