点击跳转到系统图库,然后将选择回来的图片显示到应用上
有时候需要跳转到系统图库选图,那么用以下代码实现
/**点击跳转到系统图库,然后将选择回来的图片显示到应用上*/ public class MainActivity extends Activity { private ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.iv); } //点击跳转按钮 public void click(View v){ Intent intent = new Intent(); intent.setAction(Intent.ACTION_PICK); intent.setType("image/*"); startActivityForResult(intent, 2); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode==2){ if(data!=null){//防止用户不选图片,回来时软件崩溃 Uri uri = data.getData(); iv.setImageURI(uri); } } } }
这是结果