Skip to content

ThirteeNov

My personal blog about coding and internet

Menu
  • About me
  • About Zofia Kreasi
  • Cart
  • Checkout
  • Making an airplane game from scratch in Unity
  • My account
  • Privacy Policy
  • Privacy Policy – zkLeaderboard
  • Sample Page
  • Shop
  • Tutorials on Learning JavaScript
  • ZKAccounts – Privacy Policy
Menu

Picking an Image and Taking Photo in Android – Java source code

Posted on April 27, 2018August 4, 2020 by Habibie

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" />
Post Views: 380
ciihuy2020

Welcome!

  • My YouTube Channel
  • My GitHub Page
  • About me

Categories

  • 3DVista
  • Android
  • Apache
  • C#
  • Cordova
  • Electron & Node JS
  • HTML5, CSS & JavaScript
  • iOS
  • Let's Make Unity Games
  • Misc
  • Photoshop
  • PHP
  • Python
  • Uncategorized
  • Unity
  • WordPress

Recent Posts

  • Hover Reveal script for Unity to show and hide object on mouse hover
  • How to Prevent UI Clicks in Unity from “Bleeding Through” to 3D Objects Behind Them
  • Make objects like wires and cables easily in Unity using Ciihuy Curved Mesh
  • [SOLVED] Can’t Add Custom Domain to Blogger After Losing CNAME Verification
  • iOS App Icon Generator by CiihuyCom
© 2026 ThirteeNov | Powered by Superbs Personal Blog theme