0

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 errorenter image description here

Rajesh Gosemath
  • 1,812
  • 1
  • 17
  • 31
  • 1
    check this out : http://stackoverflow.com/questions/32244851/androidjava-lang-outofmemoryerror-failed-to-allocate-a-23970828-byte-allocatio – Sar Nov 29 '16 at 09:43
  • `and the following message is displayed`. You are not even mentioning the error. And logcats should be posted as text. Not as an image. – greenapps Nov 29 '16 at 09:46
  • Pretty bad idea to fiddle aroud with Bitmaps and base64 to upload a jpg file. It can easily lead to out of memory as the bitmap will take ten times as much memory as the jpg. You probably let the bitmap compress back to jpg too. Which we dont know for sure as `compressFormat` is unknown. – greenapps Nov 29 '16 at 09:52
  • follow this code: http://stackoverflow.com/questions/5991319/capture-image-from-camera-and-display-in-activity – Gaurav Nov 29 '16 at 10:02
  • @Gaurav. There is nothing there that uploads an image file. – greenapps Nov 29 '16 at 10:05
  • @greenapps: I used BitmapFactory.Options options = new BitmapFactory.Options();options.inSampleSize = 8; and its working fine.Is it a proper way? – Rajesh Gosemath Nov 29 '16 at 10:13
  • That is you who decides. Do you want to upload that jpg file or do you want to upload a resized -smaller- image? If you want to resize the code is ok. If you want to upload the original jpg file then do not fiddle around with converting to Bitmap first. – greenapps Nov 29 '16 at 11:33
  • @greenapps:ok .Thanks a lot.. – Rajesh Gosemath Nov 29 '16 at 11:49

0 Answers0