Intent 选择器简记

正做一个选择图片来源(相册/相机拍照)功能。常规套路是弹窗选择来源,然后根据选择分别跳不同的Intent,弹窗预设使用AlertDialog或PopuWindow。想想搞个弹窗挺费事的,于是就决定使用选择器啦!

Intent carmeraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//相机intent

Intent contentIntent = new Intent(Intent.ACTION_GET_CONTENT);//检索文件intent
contentIntent.setType("*/*");

Intent galleryIntent = new Intent(Intent.ACTION_PICK);//相册intent
galleryIntent.setType("image/*");

Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);//发送信息intent
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, "你好");

Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_TITLE, "选择图片来源");
chooserIntent.putExtra(Intent.EXTRA_INTENT, galleryIntent);//这个参数不理解解,把galleryIntent换成sendIntent效果会不同。理解是干啥的留个言哈,感谢(*╹▽╹*)
//Intent chooserIntent = Intent.createChooser(galleryIntent,"选择图片来源");等于上面3行
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Parcelable[]{carmeraIntent, contentIntent, sendIntent});//这里把要跳的其它intent都加进来
startActivityForResult(chooserIntent, 100);

o(╥﹏╥)o选择器用不溜,但是经过查阅和自行脑补懂了就简单咯。
有心人天不负,天只会虐你O(∩_∩)O哈哈~

你可能感兴趣的:(Android)