Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Base64 Encoding takes too long time in Android

I'm capturing an Image with the Camera. I save the File in the public photo directory and save the Uri to that file.

I want to save the Image in a Base64 String and put it on a HashMap to put it then in a XML file later.

protected Void doInBackground(Void...voids) {
        options.inJustDecodeBounds = false;
        //Bitmap bmp = BitmapFactory.decodeFile(imageFilePath,options);
        InputStream in = null;
        try {
            in = getContentResolver().openInputStream(Uri.parse(mCurrentPhotoPath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        options.inSampleSize = 2;
        Bitmap image = BitmapFactory.decodeStream(in,null,options);
        int imgHeight = image.getHeight();
        int imgWidth = image.getWidth();
        while(imgHeight>2000){
            imgHeight = imgHeight / 2;
        }
        while(imgWidth>2000){
            imgWidth = imgWidth / 2;
        }

        Bitmap test = Bitmap.createScaledBitmap(image,imgWidth,imgHeight,false);

        String stest = base64EncodeDecode.encodeToBase64(test);


        items.put("image",base64EncodeDecode.encodeToBase64(test);
        return null;
}

The Base64 takes too long to encode it. encodeToBase64 Method

public String encodeToBase64(Bitmap image) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] b = baos.toByteArray();

    return Base64.encodeToString(b, Base64.DEFAULT);
}

Can you tell me if I do something wrong while encoding?

I hope my problem is clear.

Kind Regards!

like image 845
lainio Avatar asked Nov 09 '22 21:11

lainio


1 Answers

If you are getting !!! FAILED BINDER TRANSACTION !!! error is probably because you are passing to much data to the other Activity, there is a limit of how much you can send. Try compressing your image to 50% or 30% image.compress(Bitmap.CompressFormat.JPEG, 50, baos);

like image 92
Beelphegor Avatar answered Nov 15 '22 12:11

Beelphegor