
In this Tutorial Capture Image from camera and select photo from gallery in android.
Capture Image from Camera in Android
Capture Image from camera used to you can declared permission in camera in android manifest file and then you can access the camera and then open camera in built in your device and then capture image to stored internal or external storage.
you can declared another feature is camera hardware in android manifest file. in this feature is does not required to all devices it can be support some device hardware in camera.
you can used camera to define runtime permission to user ask is access camera or not. user can allow the permission to access the camera in your app and then user can deny permission to does not access camera in your app.
Select Photo/Image from Gallery in Android
Now You can select Image from gallery in your app feature. so you can required READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permission declared. in this permission to access you internal or external storage and then choose image from gallery.
you in this tutorial to open the context menu and then user can ask to Capture Camera and Gallery option. and user can select camera to open camera and capture the image. and then user can select gallery to open the gallery and then select image. in this image open to used Intent.
Intent add setType() and setAction() method call and then setAction method passed to Intent.ACTION_GET_CONTENT and send intent is startActivityForResult. now you can add to open gallery and then pick image from gallery.
Now Another is open camera is used to Intent and intent send to MediaStore.ACTION_IMAGE_CAPTURE. you can add this line to open camera and then captured image.
Example of Capture Photo/Image from Camera and Select Image From Gallery.
Declared Permission in AndroidManifest.xml File.
1 2 3 4 5 |
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-feature android:name="android.hardware.camera2" /> <uses-permission android:name="android.permission.CAMERA" /> |
Create activity_main.xml File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" tools:context="com.example.bhaumik.camerademo.MainActivity"> <Button android:id="@+id/btn_selected" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Selecte Image" android:textAllCaps="false" android:layout_marginTop="10dp" android:layout_gravity="center_horizontal"/> <ImageView android:id="@+id/img_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp"/> </LinearLayout> |
Create MainActivity.java File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
package com.example.bhaumik.camerademo; import android.Manifest; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.media.MediaScannerConnection; import android.net.Uri; import android.os.Build; import android.os.Environment; import android.provider.MediaStore; import android.provider.Settings; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Calendar; public class MainActivity extends AppCompatActivity { Button click; ImageView imageView; private int camera = 1,gellary = 2; public static final int PERMISSION_CODE = 111; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); click = (Button) findViewById(R.id.btn_selected); imageView = (ImageView) findViewById(R.id.img_view); if (!checkPermission()){ requestPermission(); } click.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { OpenImages(); } }); } public boolean checkPermission(){ int result = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE); int result1 = ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE); int result2 = ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.CAMERA); return result == PackageManager.PERMISSION_GRANTED && result1 == PackageManager.PERMISSION_GRANTED && result2 == PackageManager.PERMISSION_GRANTED; } public void requestPermission(){ ActivityCompat.requestPermissions(MainActivity.this,new String[] {Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CAMERA},PERMISSION_CODE); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode){ case PERMISSION_CODE : if (grantResults.length > 0){ boolean storage = grantResults[0] == PackageManager.PERMISSION_GRANTED; boolean cameras = grantResults[0] == PackageManager.PERMISSION_GRANTED; if (storage && cameras){ Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)){ showMsg("You need to allow access to the permissions", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CAMERA},PERMISSION_CODE); } } }); return; } } } } } } private void showMsg(String s, DialogInterface.OnClickListener listener) { new AlertDialog.Builder(MainActivity.this) .setMessage(s) .setPositiveButton("OK", listener) .setNegativeButton("Cancel", null) .create() .show(); } private void OpenImages() { final CharSequence[] option = {"Camera","Gellary"}; AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("Select Action"); builder.setItems(option, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (option[which].equals("Camera")){ CameraIntent(); } if (option[which].equals("Gellary")){ GellaryIntent(); } } }); AlertDialog dialog = builder.create(); dialog.show(); } private void GellaryIntent() { Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent,"Select File"),gellary); } private void CameraIntent() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent,camera); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK){ if (requestCode == camera){ OpenCameraResult(data); }else if (requestCode == gellary){ OpenGellaryResult(data); } } } private void OpenGellaryResult(Intent data) { Bitmap bitmap = null; if (data != null){ try { bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),data.getData()); } catch (IOException e) { e.printStackTrace(); } } imageView.setImageBitmap(bitmap); } private void OpenCameraResult(Intent data) { Bitmap bitmap = (Bitmap) data.getExtras().get("data"); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG,90,bytes); File paths = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg"); Toast.makeText(this, "Path -> " + paths.getAbsolutePath(), Toast.LENGTH_SHORT).show(); Log.d("tag","File Path -> " + paths.getName()); try { FileOutputStream fos = new FileOutputStream(paths); paths.createNewFile(); if(!paths.exists()){ paths.mkdir(); } fos.write(bytes.toByteArray()); fos.close(); } catch (IOException e) { e.printStackTrace(); } imageView.setImageBitmap(bitmap); } } |

In this Example of used to runtime permission in camera and storage. you can allow this permission and then you can click on button to open dialog and ask two option in camera and gallery you can choose camera and click to open the camera and captured image and display image and you can select gallery open to open gallery in you device and then select the image and display image in you app.