Selecting camera or gallery for image in Android
I had worked hard on Camera or gallery Image Selection, and created some util class for this work. With the use of these class 'Selecting image camera or gallery is too easy' it just took 5-10 mints of your development.
Step-1: Add these classes in your code.
ImagePickerUtils :- http://www.codesend.com/view/f8f7c637716bf1c693d1490635ed49b3/
BitmapUtils :- http://www.codesend.com/view/81c1c2a3f39f1f7e627f01f67be282cf/
ConvertUriToFilePath :- http://www.codesend.com/view/f4668a29860235dd1b66eb419c5a58b5/
MediaUtils :- https://codeshare.io/5vKEMl
We need to Add these Permission in menifest :
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
This class function (checkAndRequestPermissions) auto check the permission in Android-Marshmallow and Android - Nougat.
Step-2. Calling camera class for launching camera intent :
//Create a global veriable .
private Uri mCameraUri;
private static final int CAMERA_REQUEST_CODE = 100;
// Call this function when you wants to select or capture an Image.
mCameraUri = ImagePickerUtils.createTakePictureIntent(this, CAMERA_REQUEST_CODE);
Step-3: Add onActivityResult in your Activity for receiving data from Intent
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
Uri fileUri = ImagePickerUtils.getFileUriOfImage(this, data, mCameraUri);
try {
Bitmap bitmap = null;
if (CAMERA_REQUEST_CODE == requestCode) {
bitmap = new BitmapUtils().getDownsampledBitmap(this, fileUri, imageView.getWidth(), imageView.getHeight());
}
if (bitmap != null)
imageView.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
I hope it helps you, If any one having any suggestion to improve these class please add your review in comments.