When a requirement in your app, you want to pick image from gallery or image using run time permission. In this tutorial we can see how to pick image from gallery or camera.
Screen Shots:
Download Source Code
Step 1: XML Layout
Open activity_main.xml file from layout folder, add Image View and Button widgets like below.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.prasad.imagepickerex.MainActivity"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/imagePickBtn" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_margin="20dp" android:background="#E8E8E8" /> <Button android:id="@+id/imagePickBtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_margin="20dp" android:background="@color/colorPrimary" android:text="Pick Image" android:textColor="#ffffff" android:textSize="18dp" /> </RelativeLayout>
Step 2: Java Classes
Open MainActivity.java class do the changes like below.
MainActivity.java:
package com.prasad.imagepickerex; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.ImageView; import java.io.File; public class MainActivity extends AppCompatActivity implements ImageUtils.ImageAttachmentListener { ImageView imageView; Button imagePickBtn; private Bitmap bitmap; private String file_name; ImageUtils imageutils; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageutils = new ImageUtils(this); imageView = (ImageView) findViewById(R.id.imageView); imagePickBtn = (Button) findViewById(R.id.imagePickBtn); imagePickBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { imageutils.imagepicker(1); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); imageutils.onActivityResult(requestCode, resultCode, data); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { imageutils.request_permission_result(requestCode, permissions, grantResults); } @Override public void image_attachment(int from, String filename, Bitmap file, Uri uri) { bitmap = file; file_name = filename; imageView.setImageBitmap(file); String path = Environment.getExternalStorageDirectory() + File.separator + "ImagePicker" + File.separator; imageutils.createImage(file, filename, path, false); } }
Create a new class with name as ImageUtils.java, copy the below code.
ImageUtils.java:
package com.prasad.imagepickerex; import android.Manifest; import android.app.Activity; import android.content.ContentValues; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.media.ExifInterface; import android.net.Uri; import android.os.Build; import android.provider.MediaStore; 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.util.Base64; import android.util.Log; import android.widget.Toast; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; /** * Created by venkataprasad.kukka on 01-03-2017. */ public class ImageUtils { Context context; Activity current_activity; ImageAttachmentListener imageAttachment_callBack; String selected_path = ""; Uri imageUri; File path = null; int from = 0; public ImageUtils(Activity act) { this.context = act; this.current_activity = act; imageAttachment_callBack = (ImageAttachmentListener) context; } /** * Get file name from path * * @param path * @return */ public String getfilename_from_path(String path) { return path.substring(path.lastIndexOf('/') + 1, path.length()); } /** * Get Image URI from Bitmap * * @param context * @param photo * @return */ public Uri getImageUri(Context context, Bitmap photo) { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); photo.compress(Bitmap.CompressFormat.PNG, 80, bytes); String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), photo, "Title", null); return Uri.parse(path); } /** * Get Path from Image URI * * @param uri * @return */ public String getPath(Uri uri) { String[] projection = {MediaStore.Images.Media.DATA}; Cursor cursor = this.context.getContentResolver().query(uri, projection, null, null, null); int column_index = 0; if (cursor != null) { column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); String path = cursor.getString(column_index); cursor.close(); return path; } else return uri.getPath(); } /** * Bitmap from String * * @param encodedString * @return */ public Bitmap StringToBitMap(String encodedString) { try { byte[] encodeByte = Base64.decode(encodedString, Base64.DEFAULT); Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length); return bitmap; } catch (Exception e) { e.getMessage(); return null; } } /** * Get String from Bitmap * * @param bitmap * @return */ public String BitMapToString(Bitmap bitmap) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 80, baos); byte[] b = baos.toByteArray(); String temp = Base64.encodeToString(b, Base64.DEFAULT); return temp; } /** * Check Camera Availability * * @return */ public boolean isDeviceSupportCamera() { if (this.context.getPackageManager().hasSystemFeature( PackageManager.FEATURE_CAMERA)) { // this device has a camera return true; } else { // no camera on this device return false; } } /** * Compress Imgae * * @param imageUri * @param height * @param width * @return */ public Bitmap compressImage(String imageUri, float height, float width) { String filePath = getRealPathFromURI(imageUri); Bitmap scaledBitmap = null; BitmapFactory.Options options = new BitmapFactory.Options(); // by setting this field as true, the actual bitmap pixels are not loaded in the memory. Just the bounds are loaded. If // you try the use the bitmap here, you will get null. options.inJustDecodeBounds = true; Bitmap bmp = BitmapFactory.decodeFile(filePath, options); int actualHeight = options.outHeight; int actualWidth = options.outWidth; // max Height and width values of the compressed image is taken as 816x612 float maxHeight = height; float maxWidth = width; float imgRatio = actualWidth / actualHeight; float maxRatio = maxWidth / maxHeight; // width and height values are set maintaining the aspect ratio of the image if (actualHeight > maxHeight || actualWidth > maxWidth) { if (imgRatio < maxRatio) { imgRatio = maxHeight / actualHeight; actualWidth = (int) (imgRatio * actualWidth); actualHeight = (int) maxHeight; } else if (imgRatio > maxRatio) { imgRatio = maxWidth / actualWidth; actualHeight = (int) (imgRatio * actualHeight); actualWidth = (int) maxWidth; } else { actualHeight = (int) maxHeight; actualWidth = (int) maxWidth; } } // setting inSampleSize value allows to load a scaled down version of the original image options.inSampleSize = calculateInSampleSize(options, actualWidth, actualHeight); // inJustDecodeBounds set to false to load the actual bitmap options.inJustDecodeBounds = false; // this options allow android to claim the bitmap memory if it runs low on memory options.inPurgeable = true; options.inInputShareable = true; options.inTempStorage = new byte[16 * 1024]; try { // load the bitmap from its path bmp = BitmapFactory.decodeFile(filePath, options); } catch (OutOfMemoryError exception) { exception.printStackTrace(); } try { scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight, Bitmap.Config.ARGB_8888); } catch (OutOfMemoryError exception) { exception.printStackTrace(); } float ratioX = actualWidth / (float) options.outWidth; float ratioY = actualHeight / (float) options.outHeight; float middleX = actualWidth / 2.0f; float middleY = actualHeight / 2.0f; Matrix scaleMatrix = new Matrix(); scaleMatrix.setScale(ratioX, ratioY, middleX, middleY); Canvas canvas = new Canvas(scaledBitmap); canvas.setMatrix(scaleMatrix); canvas.drawBitmap(bmp, middleX - bmp.getWidth() / 2, middleY - bmp.getHeight() / 2, new Paint(Paint.FILTER_BITMAP_FLAG)); // check the rotation of the image and display it properly ExifInterface exif; try { exif = new ExifInterface(filePath); int orientation = exif.getAttributeInt( ExifInterface.TAG_ORIENTATION, 0); Log.d("EXIF", "Exif: " + orientation); Matrix matrix = new Matrix(); if (orientation == 6) { matrix.postRotate(90); Log.d("EXIF", "Exif: " + orientation); } else if (orientation == 3) { matrix.postRotate(180); Log.d("EXIF", "Exif: " + orientation); } else if (orientation == 8) { matrix.postRotate(270); Log.d("EXIF", "Exif: " + orientation); } scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true); return scaledBitmap; } catch (IOException e) { e.printStackTrace(); } return null; } /** * Get RealPath from Content URI * * @param contentURI * @return */ private String getRealPathFromURI(String contentURI) { Uri contentUri = Uri.parse(contentURI); Cursor cursor = context.getContentResolver().query(contentUri, null, null, null, null); if (cursor == null) { return contentUri.getPath(); } else { cursor.moveToFirst(); int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); return cursor.getString(index); } } /** * ImageSize Calculation * * @param options * @param reqWidth * @param reqHeight * @return */ public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } final float totalPixels = width * height; final float totalReqPixelsCap = reqWidth * reqHeight * 2; while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) { inSampleSize++; } return inSampleSize; } /** * Launch Camera * * @param from */ public void launchCamera(int from) { this.from = from; if (Build.VERSION.SDK_INT >= 23) { permission_check(1); } else { camera_call(); } } /** * Launch Gallery * * @param from */ public void launchGallery(int from) { this.from = from; if (Build.VERSION.SDK_INT >= 23) { permission_check(2); } else { galley_call(); } } /** * Show AlertDialog with the following options * <p> * Camera * Gallery * * @param from */ public void imagepicker(final int from) { this.from = from; final CharSequence[] items; if (isDeviceSupportCamera()) { items = new CharSequence[2]; items[0] = "Camera"; items[1] = "Gallery"; } else { items = new CharSequence[1]; items[0] = "Gallery"; } android.app.AlertDialog.Builder alertdialog = new android.app.AlertDialog.Builder(current_activity); alertdialog.setTitle("Add Image"); alertdialog.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int item) { if (items[item].equals("Camera")) { launchCamera(from); } else if (items[item].equals("Gallery")) { launchGallery(from); } } }); alertdialog.show(); } /** * Check permission * * @param code */ public void permission_check(final int code) { int hasWriteContactsPermission = ContextCompat.checkSelfPermission(current_activity, Manifest.permission.WRITE_EXTERNAL_STORAGE); if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) { if (!ActivityCompat.shouldShowRequestPermissionRationale(current_activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { showMessageOKCancel("For adding images , You need to provide permission to access your files", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ActivityCompat.requestPermissions(current_activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, code); } }); return; } ActivityCompat.requestPermissions(current_activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, code); return; } if (code == 1) camera_call(); else if (code == 2) galley_call(); } private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) { new AlertDialog.Builder(current_activity) .setMessage(message) .setPositiveButton("OK", okListener) .setNegativeButton("Cancel", null) .create() .show(); } /** * Capture image from camera */ public void camera_call() { ContentValues values = new ContentValues(); imageUri = current_activity.getContentResolver().insert( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent1.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); current_activity.startActivityForResult(intent1, 0); } /** * pick image from Gallery */ public void galley_call() { Intent intent2 = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); intent2.setType("image/*"); current_activity.startActivityForResult(intent2, 1); } /** * Activity PermissionResult * * @param requestCode * @param permissions * @param grantResults */ public void request_permission_result(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode) { case 1: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { camera_call(); } else { Toast.makeText(current_activity, "Permission denied", Toast.LENGTH_LONG).show(); } break; case 2: // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { galley_call(); } else { Toast.makeText(current_activity, "Permission denied", Toast.LENGTH_LONG).show(); } break; } } /** * Intent ActivityResult * * @param requestCode * @param resultCode * @param data */ public void onActivityResult(int requestCode, int resultCode, Intent data) { String file_name; Bitmap bitmap; switch (requestCode) { case 0: if (resultCode == current_activity.RESULT_OK) { Log.i("Camera Selected", "Photo"); try { selected_path = null; selected_path = getPath(imageUri); // Log.i("selected","path"+selected_path); file_name = selected_path.substring(selected_path.lastIndexOf("/") + 1); // Log.i("file","name"+file_name); bitmap = compressImage(imageUri.toString(), 816, 612); imageAttachment_callBack.image_attachment(from, file_name, bitmap, imageUri); } catch (Exception e) { e.printStackTrace(); } } break; case 1: if (resultCode == current_activity.RESULT_OK) { Log.i("Gallery", "Photo"); Uri selectedImage = data.getData(); try { selected_path = null; selected_path = getPath(selectedImage); file_name = selected_path.substring(selected_path.lastIndexOf("/") + 1); bitmap = compressImage(selectedImage.toString(), 816, 612); imageAttachment_callBack.image_attachment(from, file_name, bitmap, selectedImage); } catch (Exception e) { e.printStackTrace(); } } break; } } /** * Get image from Uri * * @param uri * @param height * @param width * @return */ public Bitmap getImage_FromUri(Uri uri, float height, float width) { Bitmap bitmap = null; try { bitmap = compressImage(uri.toString(), height, width); } catch (Exception e) { e.printStackTrace(); } return bitmap; } /** * Get filename from URI * * @param uri * @return */ public String getFileName_from_Uri(Uri uri) { String path = null, file_name = null; try { path = getRealPathFromURI(uri.getPath()); file_name = path.substring(path.lastIndexOf("/") + 1); } catch (Exception e) { e.printStackTrace(); return null; } return file_name; } /** * Check Image Exist (or) Not * * @param file_name * @param file_path * @return */ public boolean checkimage(String file_name, String file_path) { boolean flag; path = new File(file_path); File file = new File(path, file_name); if (file.exists()) { Log.i("file", "exists"); flag = true; } else { Log.i("file", "not exist"); flag = false; } return flag; } /** * Get Image from the given path * * @param file_name * @param file_path * @return */ public Bitmap getImage(String file_name, String file_path) { path = new File(file_path); File file = new File(path, file_name); BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.ARGB_8888; options.inSampleSize = 2; options.inTempStorage = new byte[16 * 1024]; Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options); if (bitmap != null) return bitmap; else return null; } /** * Create an image * * @param bitmap * @param file_name * @param filepath * @param file_replace */ public void createImage(Bitmap bitmap, String file_name, String filepath, boolean file_replace) { path = new File(filepath); if (!path.exists()) { path.mkdirs(); } File file = new File(path, file_name); if (file.exists()) { if (file_replace) { file.delete(); file = new File(path, file_name); store_image(file, bitmap); Log.i("file", "replaced"); } } else { store_image(file, bitmap); } } /** * @param file * @param bmp */ public void store_image(File file, Bitmap bmp) { try { FileOutputStream out = new FileOutputStream(file); bmp.compress(Bitmap.CompressFormat.PNG, 80, out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } } // Image Attachment Callback public interface ImageAttachmentListener { public void image_attachment(int from, String filename, Bitmap file, Uri uri); } }
Step 3: Android Manifest
Open AndroidManifest.java add the WRITE_EXTERNAL_STORAGE permission.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.prasad.imagepickerex"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Greetings. I know this is somewhat off-topic, but I was wondering if you knew where I could get a captcha plugin for my comment form? I’m using the same blog platform like yours, and I’m having difficulty finding one? Thanks a lot.
ReplyDeleteAWS Training in Bangalore | Amazon Web Services Training in Bangalore
Amazon Web Services Training in Pune | Best AWS Training in Pune
The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.
ReplyDeleteangularjs Training in bangalore
angularjs Training in btm
angularjs Training in electronic-city
angularjs Training in online
angularjs Training in marathahalli
Well somehow I got to read lots of articles on your blog. It’s amazing how interesting it is for me to visit you very often.
ReplyDeleteWell somehow I got to read lots of articles on your blog. It’s amazing how interesting it is for me to visit you very often.
python training in tambaram | python training in annanagar | python training in jayanagar
DeleteGood Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeleteJava training in Chennai | Java training in USA | Java training in Kalyan nagar
Nice post. Thanks for sharing the valuable information. it’s really helpful. Who want to learn this blog most helpful. Keep sharing on updated posts…
ReplyDeleteData Science training in kalyan nagar | Data Science training in OMR
Data Science training in chennai | Data science training in velachery
Data science training in tambaram | Data science training in jaya nagar
Thanks for such a great article here. I was searching for something like this for quite a long time and at last I’ve found it on your blog. It was definitely interesting for me to read about their market situation nowadays. Well written article Thank You for Sharing with Us pmp training in velachery | project management certfication in chennai | project management training institute in chennai | pmp training fee | pmp certification course in chennai
ReplyDeleteAll are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.
ReplyDeletedevops online training
aws online training
data science with python online training
data science online training
rpa online training
nice message thanks for sharing to us
ReplyDeleteaws training center in chennai
aws training in chennai
aws training in omr
aws training in sholinganallur
aws training institute in chennai
best aws training in sholinganallur
best angularjs training in chennai
angular js training in sholinganallur
Your topic is very nice and helpful to us … Thank you for the information you wrote.
ReplyDeleteLearn Hadoop Training from the Industry Experts we bridge the gap between the need of the industry. Bangalore Training Academy provide the Best Hadoop Training in Bangalore with 100% Placement Assistance. Book a Free Demo Today.
Big Data Analytics Training in Bangalore
Tableau Training in Bangalore
Data Science Training in Bangalore
Workday Training in Bangalore
Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts.sap abap training in bangalore
ReplyDeleteYou have outdone yourself this time. It is probably the best, most short step by step guide that I have ever seen. endzone camera
ReplyDeleteYour Website is very good, Your Website impressed us a lot, We have liked your website very much.really awsome blog...thnaks
ReplyDeleteJava training in Chennai
Java Online training in Chennai
Java Course in Chennai
Best JAVA Training Institutes in Chennai
Java training in Bangalore
Java training in Hyderabad
Java Training in Coimbatore
Java Training
Java Online Training
This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here keep up the good work dslr malayalam tutorials
ReplyDeleteThe website is looking bit flashy and it catches the visitors eyes. Design is pretty simple and a good user friendly interface. Camera BF Deals 2020
ReplyDeleteThis is such a great resource that you are providing and you give it away for free. Black Friday Deals 2020
ReplyDeleteThis is great, just love it. Also, if you are visit our website if you are looking for assistance with your nursing assignment:
ReplyDeleteNursing Assignment Help