Select image album for android and select take photo now

Source: Internet
Author: User

Public class MainActivity extends Activity implements OnClickListener {private ImageView iv; private int SELECT_PICTURE = 1; // select the image private int SELECT_CAMER = 2 from the image library; // use the camera to take the photo private Bitmap bmp; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); init ();} private void init () {iv = (ImageView) findViewById (R. id. imageVie W1); iv. setOnClickListener (this) ;}@ Overrideprotected void onActivityResult (int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubsuper. onActivityResult (requestCode, resultCode, data); if (resultCode = RESULT_ OK) {Uri uri = data. getData (); ContentResolver cr = this. getContentResolver (); try {if (bmp! = Null) {bmp. recycle (); bmp = BitmapFactory. decodeStream (cr. openInputStream (uri);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();} iv. setImageURI (uri);} else {Toast. makeText (MainActivity. this, "failed to select image, please reselect", Toast. LENGTH_SHORT ). show () ;}@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubswitch (v. getId () {case R. id. imageView1: showChoosePhotoDialog (); break; default: break;} private void showChoosePhotoDialog () {CharSequence [] items = {"album", "camera "}; alertDialog dialog = new AlertDialog. builder (this ). setTitle ("select image source "). setItems (items, new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stubif (which = SELECT_PICTURE) {Intent intent = new Intent (MediaStore. ACTION_IMAGE_CAPTURE); startActivityForResult (intent, SELECT_CAMER);} else {Intent intent = new Intent (Intent. ACTION_GET_CONTENT); intent. addCategory (Intent. CATEGORY_OPENABLE); intent. setType ("image/*"); startActivityForResult (intent, SELECT_PICTURE );}}}). setNegativeButton ("cancel", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stubdialog. cancel ();}}). create (); dialog. show ();}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.