VVB
VVB

Reputation: 7641

Why PNG forms black edges around the image for PNG format?

I select image from gallery, convert into Base64 and sends to server. For JPEG image, it works fine; the image I upload from gallery on server same gets shown in server folder. However, when I upload PNG format image from mobile gallery, it doesn't show same on server; instead it creates black edges around it. I really don't know what's going wrong?

Also, my actual image is as equal as given JPEG image.

Reference images:

JPEG:

enter image description here

PNG:

enter image description here

I just want to get rid of BLACK borders which should not appear for PNG format images.

Below is my code snippet

FileInputStream mFileInputStream = null;
        try {
            mFileInputStream = new FileInputStream(imagePathFromSDCard);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int bytesRead = 0;
            while ((bytesRead = mFileInputStream.read(b)) != -1) {
                bos.write(b, 0, bytesRead);
            }

            Bitmap bitmap = safeImageProcessing.decodeFile(uri);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);


            byte[] ba = bos.toByteArray();
            String encodedImage = Base64.encodeToString(ba, Base64.NO_WRAP);


//this line sends image base64 to server & there i decode into original
            new ImageAsync().sendImageProcess(getActivity(), encodedImage, this);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

Upvotes: 1

Views: 361

Answers (1)

greenapps
greenapps

Reputation: 11224

You want to upload image files. But you use BitmapFactory to make a Bitmap out of them first. Then you compress the bitmap to a jpg or png byte array. After that you base64 encode the bytes to a string which you upload.

What a waiste. And you have changed the files. Do away with the intermediate bitmap.

Instead load the files directly in a byte array. Then continue with the byte array as usual.

Having said that i think its a bad idea to base64 encode the bytes of a file first as it increases the amount of bytes that have to be transferred with 30%.

Upvotes: 0

Related Questions