This is Java source code for Android, providing you with two functions: to take a photo using camera and to pick an image from gallery.
//bitmap variable Bitmap bitmap; //function to take photo public void takeAPhoto(){ Intent takePic = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(takePic, 0); } //function to pick an image from gallery public void pickAnImage(){ Intent pickedImage = new Intent(MediaStore.ACTION_GET_CONTENT); startActivityForResult(pickedImage, 1); } //receiving taken photo and assign bitmap variable with it @Override protected void onActivityResult(int RC, int RQC, Intent I) { super.onActivityResult(RC, RQC, I); //if a photo has been taken if(RC == 0 && RQC == RESULT_OK){ Uri uri = I.getData(); try { bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri); } catch (IOException e) { e.printStackTrace(); } } //if an image has been picked if(RC == 1 && RQC == RESULT_OK){ Uri uri = I.getData(); try { bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri); } catch (IOException e) { e.printStackTrace(); } } }
Taking photo and picking an image from device requires these permissions on your manifest file:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
loading...