Android capture image from Camera and select image from Gallery
Camera
The Android framework includes support for various cameras and camera features available on devices, allowing you to capture pictures and videos in your applications. The Camera class is used to set image capture settings, start/stop preview, snap pictures, and retrieve frames for encoding for video. This class is a client for the Camera service, which manages the actual camera hardware.
To access the device camera, you must declare the CAMERA permission in your Android Manifest. Also be sure to include the <uses-feature> manifest element to declare camera features used by your application.
Gallery
A view that shows items in a center-locked, horizontally scrolling list.
The default values for the Gallery assume you will be using Theme_galleryItemBackground as the background for each View given to the Gallery from the Adapter. If you are not doing this, you may need to adjust some Gallery properties, such as the spacing.
Views given to the Gallery should use Gallery.LayoutParams as their layout parameters type.
This Tutorial discusses a quick, simple approach to capture image from camera and select image from gallery.
Step by Step Tutorial
Step 1: Open Android Studio.
Step 2: Create a new project as follows:
If you see the Welcome to Android Studio dialog, choose Start a new Android Studio project, available under ‘Quick Start’ on the right of the dialog.
Otherwise, click File in the Android Studio menu bar, then New, New Project.
Step 3: Enter your app name, company domain, and project location, as prompted. Then click Next.
Step 4: Select the form factors you need for your app. If you’re not sure what you need, just select Phone and Tablet. Then click Next.
Step 5: Select Blank Activity in the ‘Add an activity to Mobile’ dialog. Then click Next.
Step 6: Enter the activity name, layout name and title as prompted. The default values are fine. Then click Finish.
After that wait for few seconds. Android Studio starts Gradle and builds of your project.
Step 7: First of all adding permission in Android Manifest xml file.
<uses-permission android:name="android.permission.CAMERA"></uses-permission> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"></uses-permission>
Step 8: make small changes in default floating button image icon on activity_add_image.xml file. i changed mail icon into camera icon
<android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" app:srcCompat="@android:drawable/ic_menu_camera" /> <include layout="@layout/content_add_image" />
Step 9: Now add a ImageView control in content_add_image.xml file.
<ImageView android:id="@+id/ivImage" android:layout_width="match_parent" android:layout_height="match_parent" />
Step 10: Create a method to show alert dialog builder in AddImageActivity.xml (or whatever you named)
private void SelectImage(){ final CharSequence[] items={"Camera","Gallery", "Cancel"}; AlertDialog.Builder builder = new AlertDialog.Builder(AddImageActivity.this); builder.setTitle("Add Image"); builder.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { if (items[i].equals("Camera")) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, REQUEST_CAMERA); } else if (items[i].equals("Gallery")) { Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); intent.setType("image/*"); //startActivityForResult(intent.createChooser(intent, "Select File"), SELECT_FILE); startActivityForResult(intent, SELECT_FILE); } else if (items[i].equals("Cancel")) { dialogInterface.dismiss(); } } }); builder.show(); }
Step 11: Now create onActivityResult() method in same file AddImageActivity.xml
@Override public void onActivityResult(int requestCode, int resultCode, Intent data){ super.onActivityResult(requestCode, resultCode,data); if(resultCode== Activity.RESULT_OK){ if(requestCode==REQUEST_CAMERA){ Bundle bundle = data.getExtras(); final Bitmap bmp = (Bitmap) bundle.get("data"); ivImage.setImageBitmap(bmp); }else if(requestCode==SELECT_FILE){ Uri selectedImageUri = data.getData(); ivImage.setImageURI(selectedImageUri); } } }
Full Source Code
AddImageActivity.xml
import android.app.Activity; import android.content.CursorLoader; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.ActionBar; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.ImageView; public class AddImageActivity extends AppCompatActivity { ImageView ivImage; Integer REQUEST_CAMERA=1, SELECT_FILE=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add_image); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); ivImage = (ImageView) findViewById(R.id.ivImage); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { SelectImage(); } }); } private void SelectImage(){ final CharSequence[] items={"Camera","Gallery", "Cancel"}; AlertDialog.Builder builder = new AlertDialog.Builder(AddImageActivity.this); builder.setTitle("Add Image"); builder.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { if (items[i].equals("Camera")) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, REQUEST_CAMERA); } else if (items[i].equals("Gallery")) { Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); intent.setType("image/*"); startActivityForResult(intent, SELECT_FILE); } else if (items[i].equals("Cancel")) { dialogInterface.dismiss(); } } }); builder.show(); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data){ super.onActivityResult(requestCode, resultCode,data); if(resultCode== Activity.RESULT_OK){ if(requestCode==REQUEST_CAMERA){ Bundle bundle = data.getExtras(); final Bitmap bmp = (Bitmap) bundle.get("data"); ivImage.setImageBitmap(bmp); }else if(requestCode==SELECT_FILE){ Uri selectedImageUri = data.getData(); ivImage.setImageURI(selectedImageUri); } } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_add_image, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.suraj.androidimagetutorial"> <uses-permission android:name="android.permission.CAMERA"></uses-permission> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"></uses-permission> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".AddImageActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
activity_add_image.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context="com.example.suraj.androidimagetutorial.AddImageActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" app:srcCompat="@android:drawable/ic_menu_camera" /> <include layout="@layout/content_add_image" /> </android.support.design.widget.CoordinatorLayout>
content_add_image.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/content_add_image" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.suraj.androidimagetutorial.AddImageActivity" tools:showIn="@layout/activity_add_image"> <ImageView android:id="@+id/ivImage" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
One thought on “Android capture image from Camera and select image from Gallery”
there’s an error in menu_add_image. how can i fix it?