Step-1
private void captureCameraImage() {
try {
PackageManager pm = getPackageManager();
int hasPerm = pm.checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, getPackageName());
if (hasPerm == PackageManager.PERMISSION_GRANTED) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile;
try {
photoFile = AppUtils.getInstance().createImageFile("tenancy_image");
if (photoFile != null) {
Uri photoURI;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
photoURI = Uri.fromFile(photoFile);
} else {
photoURI = FileProvider.getUriForFile(this,
getBaseContext().getPackageName() + ".provider",
photoFile);
}
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
mPhotoFile = photoFile;
takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(takePictureIntent, PICK_IMAGE_CAMERA);
}
} catch (IOException e) {
e.getMessage();
}
}
} else
AppUtils.getInstance().showSnack(scrollView, "Camera Permission error");
} catch (Exception e) {
AppUtils.getInstance().showSnack(scrollView, "Camera Permission error");
e.printStackTrace();
}
}
Step-2
if (requestCode == PICK_IMAGE_CAMERA && resultCode == RESULT_OK) {try {
mPhotoFile = mCompressor.compressToFile(mPhotoFile);
InputStream ims = new FileInputStream(mPhotoFile);
Bitmap bitmap = BitmapFactory.decodeStream(ims);
imgCamera.setVisibility(View.GONE);
imgView.setImageBitmap(bitmap);
validateSubmitActivation();
} catch (FileNotFoundException e) {
e.getMessage();
return;
} catch (IOException e) {
e.printStackTrace();
}}
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class ImageUtil {
private ImageUtil() {
}
static File compressImage(File imageFile, int reqWidth, int reqHeight,
Bitmap.CompressFormat compressFormat, int quality, String destinationPath)
throws IOException {
FileOutputStream fileOutputStream = null;
File file = new File(destinationPath).getParentFile();
if (!file.exists()) file.mkdirs();
try {
fileOutputStream = new FileOutputStream(destinationPath);
// write the compressed bitmap at the destination specified by destinationPath.
decodeSampledBitmapFromFile(imageFile, reqWidth, reqHeight).compress(compressFormat, quality,
fileOutputStream);
} finally {
if (fileOutputStream != null) {
fileOutputStream.flush();
fileOutputStream.close();
}
}
return new File(destinationPath);
}
static Bitmap decodeSampledBitmapFromFile(File imageFile, int reqWidth, int reqHeight)
throws IOException {
// First decode with inJustDecodeBounds=true to check dimensions
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imageFile.getAbsolutePath(), options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap scaledBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), options);
//check the rotation of the image and display it properly
ExifInterface exif;
exif = new ExifInterface(imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
Matrix matrix = new Matrix();
if (orientation == 6) {
matrix.postRotate(90);
} else if (orientation == 3) {
matrix.postRotate(180);
} else if (orientation == 8) {
matrix.postRotate(270);
}
scaledBitmap =
Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(),
matrix, true);
return scaledBitmap;
}
private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth,
int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight & (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
}
import android.content.Context;
import android.graphics.Bitmap;
import java.io.File;
import java.io.IOException;
public class FileCompressor {
//max width and height values of the compressed image is taken as 612x816
private int maxWidth = 612;
private int maxHeight = 816;
private Bitmap.CompressFormat compressFormat = Bitmap.CompressFormat.JPEG;
private int quality = 80;
private String destinationDirectoryPath;
public FileCompressor(Context context) {
destinationDirectoryPath = context.getCacheDir().getPath() + File.separator + "images";
}
public FileCompressor setMaxWidth(int maxWidth) {
this.maxWidth = maxWidth;
return this;
}
public FileCompressor setMaxHeight(int maxHeight) {
this.maxHeight = maxHeight;
return this;
}
public FileCompressor setCompressFormat(Bitmap.CompressFormat compressFormat) {
this.compressFormat = compressFormat;
return this;
}
public FileCompressor setQuality(int quality) {
this.quality = quality;
return this;
}
public FileCompressor setDestinationDirectoryPath(String destinationDirectoryPath) {
this.destinationDirectoryPath = destinationDirectoryPath;
return this;
}
public File compressToFile(File imageFile) throws IOException {
return compressToFile(imageFile, imageFile.getName());
}
public File compressToFile(File imageFile, String compressedFileName) throws IOException {
return ImageUtil.compressImage(imageFile, maxWidth, maxHeight, compressFormat, quality,
destinationDirectoryPath + File.separator + compressedFileName);
}
public Bitmap compressToBitmap(File imageFile) throws IOException {
return ImageUtil.decodeSampledBitmapFromFile(imageFile, maxWidth, maxHeight);
}}