Camera Upload Util
public class ImageUploadUtil {
private static File imageFolder;
private static File sdcardDir = Environment.getExternalStorageDirectory();
private static String photoDir = "photos";
private static String rootPath;
private static String filePath;
public static final int ACTION_FRONT_PHOTO = 100;
public static final int ACTION_BACK_PHOTO = 200;
public static final int ACTION_SELFIE_PHOTO = 300;
public static final int REQUEST_CODE_IMAGE_CAMERA = 0;
public static void openCamera(Fragment fragment, int action) {
filePath = null;
rootPath = sdcardDir + File.separator
+ fragment.getActivity().getResources().getString(R.string.app_name);
String photoPath = rootPath + File.separator + photoDir;
// Create Directory App
imageFolder = new File(photoPath);
imageFolder.mkdirs();
openStandardCamera(action, fragment);
// // close for custom camera
// try {
// Intent camera = new Intent(fragment.getActivity(), CameraActivity.class);
// camera.putExtra(IConfig.IS_CAMERA_SELFIE, (action == ImageUploadUtil.ACTION_SELFIE_PHOTO));
// fragment.startActivityForResult(camera, action + REQUEST_CODE_IMAGE_CAMERA);
// }catch (Exception e){
// openStandardCamera(action, fragment);
// }
}
public static void openStandardCamera(int action, Fragment fragment) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
int cameraType = (action == ImageUploadUtil.ACTION_SELFIE_PHOTO) ? Camera.CameraInfo.CAMERA_FACING_FRONT : Camera.CameraInfo.CAMERA_FACING_BACK;
takePictureIntent.putExtra("android.intent.extras.CAMERA_FACING", cameraType);
if (takePictureIntent.resolveActivity(fragment.getActivity().getPackageManager()) != null) {
File filePhoto = getPicturePath(imageFolder);
Uri fileUri = FileProvider.getUriForFile(fragment.getContext(), fragment.getContext().getApplicationContext().getPackageName() + ".provider", filePhoto);
filePath = fileUri.toString();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
fragment.startActivityForResult(takePictureIntent, action + REQUEST_CODE_IMAGE_CAMERA);
}
}
private static File getPicturePath(File photoFolder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date());
String photoFile = "Picture_" + date + ".jpg";
String filename = photoFolder.getPath() + File.separator + photoFile;
return new File(filename);
}
public static Bitmap onCaptureCameraResult(Intent data, String preferenceKey, Context context) {
Bitmap thumbnail = null;
Uri thumbnailUri;
try {
thumbnailUri = (Uri) data.getExtras().get("output");
} catch (Exception e) {
try {
thumbnailUri = Uri.parse(filePath);
} catch (Exception e2) {
return null;
}
}
try {
thumbnail = MediaStore.Images.Media.getBitmap(context.getContentResolver(), thumbnailUri);
if (preferenceKey.equals(IConfig.BASE64_CAMERA_SELFIE_KEY)) {
Matrix mtx = new Matrix();
// rotate photo if below "ANDROID_VERSION_OF_LOLLIPOP"
if (FilterUtil.isAndroidVersion(FilterUtil.ANDROID_VERSION_OF_LOLLIPOP, FilterUtil.EQUAL_OR_ABOVE)
|| FilterUtil.isAndroidVersion(Build.VERSION_CODES.JELLY_BEAN, FilterUtil.EQUAL))
mtx.postRotate(0);
else
mtx.postRotate(-90);
Bitmap bitmap = resizeImage(Bitmap.createBitmap(thumbnail, 0, 0, thumbnail.getWidth(), thumbnail.getHeight(), mtx, true));
AppPreferenceUtil.putPreferenceString(preferenceKey, convertImageToBase64(bitmap), context);
thumbnail = bitmap;
} else {
AppPreferenceUtil.putPreferenceString(preferenceKey, convertImageToBase64(resizeImage(thumbnail)), context);
}
} catch (IOException e) {
e.printStackTrace();
}
return thumbnail;
}
public static Bitmap converBase64ToImage(String base64){
byte[] decodedString = Base64.decode(base64, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
return decodedByte;
}
public static String convertImageToBase64(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);
byte[] image = stream.toByteArray();
return Base64.encodeToString(image, 0);
}
public static Bitmap resizeImage(Bitmap bm){
int fixWidth = 800;
int finalHeight = bm.getHeight();
if (finalHeight > fixWidth) {
//scale height to maintain aspect ratio
finalHeight = (fixWidth * bm.getHeight()) / bm.getWidth();
}
return Bitmap.createScaledBitmap(bm, fixWidth, finalHeight, true);
}
}