The Digital Ad Venture
The Digital Ad Venture

Reputation: 1606

Android picture has red tint after decoding and pulling the .png file from the emulator to pc

I am transferring some data from a server ( java app ) to client ( android app ). The data gets Base64 encoded, sent, received correct, decoded ( correct ? ) and stored to the device ( correct ? )

I am using android studio and an AVD to simulate it. I take the pictures via DDMS from the virtual device folder to my computers harddisk in order to take a look at them. Is maybe there the problem?

now in the following code sections the picture files get decoded and stored to the device. Cant figure out where the mistake is.

Would be glad about any hint.

orignial

after decoding and storing it to device

         byte[] imageBackToByt = Base64.decode(parts[9], Base64.DEFAULT);

         Bitmap bitmapImage = BitmapFactory.decodeByteArray(imageBackToByt, 0, imageBackToByt.length);

         File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
                        + "/Android/data/"
                        + ctx.getApplicationContext().getPackageName()
                        + "/Files");

         File imageFile = new File(mediaStorageDir.getPath() + File.separator + voReceived.name + ".png");

                try {
                    FileOutputStream fos = new FileOutputStream(imageFile);
                    bitmapImage.compress(Bitmap.CompressFormat.PNG, 90, fos);
                    fos.close();
                } catch (FileNotFoundException e) {
                    Log.d(ctx.getString(R.string.SLDMP), "File not found: " + e.getMessage());
                } catch (IOException e) {
                    Log.d(ctx.getString(R.string.SLDMP), "Error accessing file: " + e.getMessage());
                }

This is how i encode it on the server in JAVA:

            BufferedImage originalPicture = null;
            ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
            byte[] pictureInByte = null;
            String pictureEncoded = null;

            try {
                // load the original picture from the filepath
                originalPicture = ImageIO.read(picturFile);

                // Convert the original picture from .png format to byte array (byte []) format
                ImageIO.write(originalPicture, "jpg", byteArrayOS );
                pictureInByte = byteArrayOS.toByteArray();

                // Encode the byte array pictureInByte to String based on Base64 encoding
                pictureEncoded = Base64.getEncoder().encodeToString(pictureInByte);

            } catch (IOException e) {

                e.printStackTrace();
                // If picture failed to load / encode store string "PICTUREERROR" as an error code
                pictureEncoded = "PICTUREERROR";

            }

Upvotes: 0

Views: 345

Answers (1)

greenapps
greenapps

Reputation: 11224

The server puts the bytes of the image file in a buffer and sends the contents of the base 64 encoded buffer to the client. Now on client side you should directly decode base 64 all bytes and write all the resulting bytes to file. In this way you have exactly the same file. All bytes are the same and file size would be equal too.

Instead you use BitmapFactory to construct a Bitmap and then compress it to PNG. That all makes no sense.

If you want to transfer a file then do not use BitmapFactory and Bitmap.

Having said that.. Mmmmm nice filter! The result is wonderfull!

Upvotes: 1

Related Questions