After capturing image from camera the app stops with the errors shown in the logcat.I think this is related with the size of the image captured.Do i need to compress image before uploading it to server. Here is the relevant code
private void openCameraIntent() {
createImageFile();
Utils.getInstance().openCamera(this,profileImageFile);
}
private void createImageFile() {
try{
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
profileImageFile = Environment.getExternalStorageDirectory()
+ "/StoredImages/"+"img" + timeStamp + ".jpeg";
}catch(Exception e){
e.printStackTrace();
}
}
openCamera Method
public void openCamera(Fragment fragment,String mediaFile) {
File mediaStorageDir = new File(
Environment.getExternalStorageDirectory()
+ "/StoredImages/");
if (!mediaStorageDir.exists()) {
mediaStorageDir.mkdirs();
}
try{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri camCapfileUri = Uri.fromFile(new File(mediaFile));
intent.putExtra(MediaStore.EXTRA_OUTPUT, camCapfileUri);
fragment.startActivityForResult(intent, StringConstants.CAMERA_PERMISSION_REQUEST_CODE);
}catch (Exception e){
e.printStackTrace();
}
}
onActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == StringConstants.CAMERA_PERMISSION_REQUEST_CODE &&
resultCode == Activity.RESULT_OK){
onCameraImageCapture();
}else if(requestCode == StringConstants.PERMISSION_REQUESTCODE_GALLERY && resultCode == Activity.RESULT_OK){
onGalleryImageSelected(data);
}
}
after image is captured it is converted into String like this
private void onCameraImageCapture() {
profileBitMap = BitmapFactory.decodeFile(profileImageFile);
String x = Utils.getInstance().encodeToBase64(profileBitMap,Bitmap.CompressFormat.JPEG,100);
uploadProfileImage();
}
public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality) {
try{
ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
image.compress(compressFormat, quality, byteArrayOS);
return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
}catch(Exception e){
e.printStackTrace();
}
return null;
}
and the stringis sent as a parameter to our server to save it.But it shows following error