通过intent来调用手机相册程序,从而选择图片。
//调用手机相册,选择图片
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent, 100);
重写onActivityResult方法,取得选择的图片。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK&& requestCode == 100)
{
if(data!=null)
{
//将选择的图片设置到imageView中
ivPicture.setImageURI(data.getData());
}
}
}
运行效果:
选择图片前:
点击上面的正方形图片将启动intent,跳到选择图片界面,如下图:
点击选择一张图片后,选中的图片将被设置到刚才的imageView中,如下图:
8971

被折叠的 条评论
为什么被折叠?



