Most of the android application will have a functionality to populate an imageview from camera or gallery.In this tutorial we will learn how to launch a camera application from your android app, how to populate Imageview with image populated from camera and how to select image from gallery and populate the Imageview in your android app.
Following is the code for launching camera
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File f = new File(android.os.Environment.getExternalStorageDirectory(), "codesfor.jpg"); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); startActivityForResult(intent, 1);
Following is the code for launching Gallery
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, 2);
The results of the above two operations will be captured in your OnActivityResult method
Let us see the full code,In this tutorial we will show a popup to select camera or Gallery while clicking on floating button.
1.Creating New Project
- Create a new project in Android Studio from File -> New Project
- Fill the required fields like Application name and package name and click Next
- Select Basic activity and click Next, and click Finish Button.A default activity will be created asMainActivity.java with layouts activity_main.xml and content_main.xml
2. Here is the code for your activity_main.xml
activity_main.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="search.com.codesforcamera.MainActivity"> <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> <include layout="@layout/content_main" /> <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" android:src="@android:drawable/ic_menu_camera" /> </android.support.design.widget.CoordinatorLayout>
3.Here is the code for content_main.xml
content_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp"> <ImageView android:id="@+id/im_image" android:layout_width="match_parent" android:layout_height="300dp" android:layout_centerVertical="true" /> </RelativeLayout>
4.Now add the following code to your MainActivity.java
MainActivity.java
public class MainActivity extends AppCompatActivity { Toolbar toolbar; ImageView im_image; FloatingActionButton fab_camera; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); fab_camera.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Choose(); } }); } private void init() { toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); im_image = (ImageView) findViewById(R.id.im_image); fab_camera = (FloatingActionButton) findViewById(R.id.fab); } private void Choose() { final CharSequence[] options_array = { "Camera", "Gallery" }; AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("Choose"); builder.setItems(options_array, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int item) { if (options_array[item].equals("Camera")) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File f = new File(android.os.Environment.getExternalStorageDirectory(), "codesfor.jpg"); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); startActivityForResult(intent, 1); } else if (options_array[item].equals("Gallery")) { Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, 2); } } }); builder.show(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { if (requestCode == 1) { File f = new File(Environment.getExternalStorageDirectory().toString()); for (File temp : f.listFiles()) { if (temp.getName().equals("codesfor.jpg")) { f = temp; break; } } try { Bitmap bitmap; BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), bitmapOptions); im_image.setImageBitmap(bitmap); } catch (Exception e) { e.printStackTrace(); } } else if (requestCode == 2) { Uri selectedImage = data.getData(); String[] filePath = { MediaStore.Images.Media.DATA }; Cursor c = getContentResolver().query(selectedImage,filePath, null, null, null); c.moveToFirst(); int columnIndex = c.getColumnIndex(filePath[0]); String picturePath = c.getString(columnIndex); c.close(); Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath)); im_image.setImageBitmap(thumbnail); } } } }
5. Following permissions are needed to be added in your AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
If you are using targetSdkVersion greater than 22 in your gradle.build file ,then you need to add the permissions externally.
output
One Comment