Site icon C1CTech

Android Capture Image From Camera And Gallery

<p>In this tutorial i will demonstrate you how to capture an image from camera and pick an image from gallery and then display it in an imageView by creating a simple <strong><span style&equals;"color&colon; &num;0000ff&semi;">ImagePickerApp<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p><img class&equals;"aligncenter wp-image-760 size-full" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;08&sol;Screenshot&lowbar;2018-08-21-15-40-291&period;png" alt&equals;"" width&equals;"480" height&equals;"800" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;">Create new project<&sol;span><&sol;h3>&NewLine;<p><strong>1<&sol;strong>&period; Create a new project in <span style&equals;"color&colon; &num;008000&semi;"><strong>Android Studio<&sol;strong><&sol;span> from <span style&equals;"color&colon; &num;008000&semi;"><strong>File &Implies; New Project<&sol;strong><&sol;span> by filling the required details&period; When it prompts you to select the activity&comma; choose <span style&equals;"color&colon; &num;008000&semi;"><strong>Empty Activity<&sol;strong><&sol;span> and continue&period;<&sol;p>&NewLine;<p><strong>2<&sol;strong>&period; Open <strong><span style&equals;"color&colon; &num;008000&semi;">AndroidManifest&period;xml<&sol;span><&sol;strong> and add the following permissions &colon;<&sol;p>&NewLine;<pre><code>&lt&semi;uses-feature&NewLine; android&colon;name&equals;"android&period;hardware&period;camera"&NewLine; android&colon;required&equals;"true" &sol;&gt&semi;<&sol;code><&sol;pre>&NewLine;<p>To advertise that your application depends on having a camera&comma; put a <span style&equals;"color&colon; &num;008000&semi;"><strong>&lt&semi;uses-feature&gt&semi; <&sol;strong><&sol;span>tag of <strong><span style&equals;"color&colon; &num;008000&semi;">name&equals;&&num;8221&semi;android&period;hardware&period;camera&&num;8221&semi;<&sol;span> <&sol;strong>in your manifest file&period;By adding <span style&equals;"color&colon; &num;008000&semi;"><strong>android&period;hardware&period;camera<&sol;strong><&sol;span>&comma; Play Store detects and prevents installing the application on devices with no camera&period;<&sol;p>&NewLine;<p>By setting <span style&equals;"color&colon; &num;008000&semi;"><strong>android&colon;required<&sol;strong><&sol;span> to <strong><span style&equals;"color&colon; &num;008000&semi;">false<&sol;span><&sol;strong> &comma;Google Play will allow devices without a camera to download your application&period;<&sol;p>&NewLine;<p><strong>3<&sol;strong>&period; Open <span style&equals;"color&colon; &num;008000&semi;"><strong>activity&lowbar;main&period;xml<&sol;strong> <&sol;span>and write the below code&period;It contains one <span style&equals;"color&colon; &num;0000ff&semi;"><strong>imageView<&sol;strong><&sol;span> and two buttons <strong><span style&equals;"color&colon; &num;0000ff&semi;">Camera<&sol;span><&sol;strong> and <strong><span style&equals;"color&colon; &num;0000ff&semi;">Gallery<&sol;span><&sol;strong> &comma;<strong>btn&lowbar;camera<&sol;strong> is to capture image from camera and <strong>btn&lowbar;gallery<&sol;strong> is to pick image from gallery&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>activity&lowbar;main&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<pre><code>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;LinearLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;margin&equals;"10dp"&NewLine; android&colon;orientation&equals;"vertical"&NewLine;&NewLine; &gt&semi;&NewLine;&NewLine; &lt&semi;ImageView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;image"&NewLine; android&colon;layout&lowbar;width&equals;"250dp"&NewLine; android&colon;layout&lowbar;height&equals;"250dp"&NewLine; android&colon;layout&lowbar;gravity&equals;"center"&NewLine; android&colon;layout&lowbar;marginTop&equals;"10dp"&NewLine; android&colon;background&equals;"&commat;drawable&sol;background"&NewLine; android&colon;scaleType&equals;"centerCrop" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;LinearLayout&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;gravity&equals;"center&lowbar;horizontal"&NewLine; android&colon;layout&lowbar;marginTop&equals;"20dp"&NewLine; android&colon;orientation&equals;"horizontal"&gt&semi;&NewLine;&NewLine; &lt&semi;Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;camera"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;marginRight&equals;"8dp"&NewLine; android&colon;layout&lowbar;weight&equals;"1"&NewLine; android&colon;background&equals;"&commat;color&sol;colorPrimary"&NewLine; android&colon;text&equals;"camera"&NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;white"&NewLine; android&colon;textSize&equals;"18sp"&NewLine; android&colon;textStyle&equals;"bold" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;gallery"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;marginLeft&equals;"8dp"&NewLine; android&colon;layout&lowbar;weight&equals;"1"&NewLine; android&colon;background&equals;"&commat;color&sol;colorPrimary"&NewLine; android&colon;text&equals;"gallery"&NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;white"&NewLine; android&colon;textSize&equals;"18sp"&NewLine; android&colon;textStyle&equals;"bold" &sol;&gt&semi;&NewLine;&NewLine;&NewLine; &lt&semi;&sol;LinearLayout&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;LinearLayout&gt&semi;<&sol;code><&sol;pre>&NewLine;<p>4&period; Open <strong><span style&equals;"color&colon; &num;008000&semi;">MainActivity&period;java<&sol;span><&sol;strong> and write the below code&colon;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>MainActivity&period;java<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>package com&period;example&period;cameratest&semi;&NewLine;&NewLine;import androidx&period;appcompat&period;app&period;AppCompatActivity&semi;&NewLine;&NewLine;import android&period;content&period;Intent&semi;&NewLine;import android&period;graphics&period;Bitmap&semi;&NewLine;import android&period;net&period;Uri&semi;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;provider&period;MediaStore&semi;&NewLine;import android&period;view&period;View&semi;&NewLine;import android&period;widget&period;Button&semi;&NewLine;import android&period;widget&period;ImageView&semi;&NewLine;&NewLine;public class MainActivity extends AppCompatActivity &lbrace;&NewLine;&NewLine; ImageView imageView&semi;&NewLine; Button btn&lowbar;camera&comma; btn&lowbar;gallery&semi;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;&NewLine;&NewLine; imageView &equals; &lpar;ImageView&rpar; findViewById&lpar;R&period;id&period;image&rpar;&semi;&NewLine; btn&lowbar;camera &equals; &lpar;Button&rpar; findViewById&lpar;R&period;id&period;btn&lowbar;camera&rpar;&semi;&NewLine; btn&lowbar;gallery &equals; &lpar;Button&rpar; findViewById&lpar;R&period;id&period;btn&lowbar;gallery&rpar;&semi;&NewLine;&NewLine;&NewLine; btn&lowbar;camera&period;setOnClickListener&lpar;new View&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public void onClick&lpar;View v&rpar; &lbrace;&NewLine; &sol;&sol;To take picture from camera&NewLine;&NewLine; Intent takePicture &equals; new Intent&lpar;MediaStore&period;ACTION&lowbar;IMAGE&lowbar;CAPTURE&rpar;&semi;&NewLine; startActivityForResult&lpar;takePicture&comma; 0&rpar;&semi;&sol;&sol;zero can be replaced with any action code&NewLine;&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine; btn&lowbar;gallery&period;setOnClickListener&lpar;new View&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public void onClick&lpar;View v&rpar; &lbrace;&NewLine; &sol;&sol;To pick photo from gallery&NewLine;&NewLine; Intent pickPhoto &equals; new Intent&lpar;Intent&period;ACTION&lowbar;PICK&comma;&NewLine; android&period;provider&period;MediaStore&period;Images&period;Media&period;EXTERNAL&lowbar;CONTENT&lowbar;URI&rpar;&semi;&NewLine; startActivityForResult&lpar;pickPhoto&comma; 1&rpar;&semi;&sol;&sol;one can be replaced with any action code&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; protected void onActivityResult&lpar;int requestCode&comma; int resultCode&comma; Intent imageReturnedIntent&rpar; &lbrace;&NewLine; super&period;onActivityResult&lpar;requestCode&comma; resultCode&comma; imageReturnedIntent&rpar;&semi;&NewLine; switch &lpar;requestCode&rpar; &lbrace;&NewLine; case 0&colon;&NewLine; if &lpar;resultCode &equals;&equals; RESULT&lowbar;OK&rpar; &lbrace;&NewLine; Bundle extras &equals; imageReturnedIntent&period;getExtras&lpar;&rpar;&semi;&NewLine; Bitmap imageBitmap &equals; &lpar;Bitmap&rpar; extras&period;get&lpar;"data"&rpar;&semi;&NewLine; imageView&period;setImageBitmap&lpar;imageBitmap&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; break&semi;&NewLine; case 1&colon;&NewLine; if &lpar;resultCode &equals;&equals; RESULT&lowbar;OK&rpar; &lbrace;&NewLine; Uri selectedImage &equals; imageReturnedIntent&period;getData&lpar;&rpar;&semi;&NewLine; imageView&period;setImageURI&lpar;selectedImage&rpar;&semi;&NewLine; &rcub;&NewLine; break&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<p>Intent is the standard way to delegate actions to another application&period;<&sol;p>&NewLine;<ul>&NewLine;<li>To start the native camera the Intent requires <span style&equals;"color&colon; &num;008000&semi;"><strong>android&period;provider&period;MediaStore&period;ACTION&lowbar;IMAGE&lowbar;CAPTURE<&sol;strong><&sol;span>&period;<&sol;li>&NewLine;<li>To pick an image from gallery&comma; the Intent requires the following argument &colon; <span style&equals;"color&colon; &num;008000&semi;"><strong>Intent&period;ACTION&lowbar;PICK<&sol;strong><&sol;span>&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>Now we know how to capture and pick image from camera and gallery now its time to set image in an <strong>imageView<&sol;strong>&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">startActivityForResult<&sol;span><&sol;strong>&lpar;&rpar; contains intent and related request code returns an intent to <span style&equals;"color&colon; &num;008000&semi;"><strong>onActivityResult<&sol;strong><&sol;span>&lpar;&rpar; &period;Inside onActivityResult&lpar;&rpar; we get the Uri from the Intent and set it in <span style&equals;"color&colon; &num;008000&semi;"><strong>imageView<&sol;strong><&sol;span> as shown below&colon;<&sol;p>&NewLine;<pre><code>Uri selectedImage &equals; imageReturnedIntent&period;getData&lpar;&rpar;&semi;&NewLine;imageView&period;setImageURI&lpar;selectedImage&rpar;&semi;<&sol;code><&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>When you run your app it will look like this as shown below&colon;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><img class&equals;"alignnone size-medium wp-image-759" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;08&sol;Screenshot&lowbar;2018-08-21-15-35-431-180x300&period;png" alt&equals;"" width&equals;"180" height&equals;"300" &sol;>      <img class&equals;"alignnone size-medium wp-image-760" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;08&sol;Screenshot&lowbar;2018-08-21-15-40-291-180x300&period;png" alt&equals;"" width&equals;"180" height&equals;"300" &sol;><&sol;p>&NewLine;<p>I hope this post is helpful for you in understanding how to <strong><span style&equals;"color&colon; &num;0000ff&semi;">Capture Image From Camera and Select Image From Gallery<&sol;span><&sol;strong> in an android application&period;&NewLine;

Exit mobile version