-1

I am getting the exception as in the Title while sending an image to a java server

Here's the code:

       ByteArrayOutputStream stream = new ByteArrayOutputStream();
       img.compress(Bitmap.CompressFormat.PNG, 100, stream);
       byte[] byteArray = stream.toByteArray();

       String imageDataString = new String(Base64.encodeBase64(byteArray)); 
       System.out.println(imageDataString);

       dataOutputStream.writeUTF(imageDataString);
       dataOutputStream.flush();

Where img is a bitmap file.

Any help will be highly appreciated !

Saaram
  • 337
  • 3
  • 7
  • 29
  • does it work for smaller images? how do you restore image at server from string?? – Nikolay Kuznetsov Dec 09 '12 at 11:11
  • what is type of dataOutputStream? – Nikolay Kuznetsov Dec 09 '12 at 11:15
  • image is not being sent because the decode string is too long according to the exception but when i send some other encode string like `string = "some string" it gets send – Saaram Dec 09 '12 at 11:16
  • @kuznetsov there is a hint for encode and decoding image into base64 string and i have done it for small images http://stackoverflow.com/questions/13785594/writing-decoded-base64-byte-array-as-image-file/13785647#13785647 – Amir Qayyum Khan Dec 09 '12 at 11:16
  • does it succeed with any 1kb and less images at all? the approach of sending image using Strings is abnormal. – Nikolay Kuznetsov Dec 09 '12 at 11:19
  • This true String has limitations , The maximum chars a String can hold is equal to maximum size of integer. – Amir Qayyum Khan Dec 09 '12 at 11:20
  • yeah it was succeed but you are rigth it is not good approach and has limitations, only images with max size one MB or 2 (i dont remember this time) can be able to sent, so i replaced it with multipart request – Amir Qayyum Khan Dec 09 '12 at 11:21
  • @Saaram use my code, you will be able to encode and decode , I dint it for android client and java server – Amir Qayyum Khan Dec 09 '12 at 11:23
  • Tell us how server processes that data, we cannot guess – Nikolay Kuznetsov Dec 09 '12 at 11:25
  • @amir qayyum khan I am doing the same as you android client and java server... Where can I find your code ? – Saaram Dec 09 '12 at 11:27
  • @AmirQayyumKhan The limitation here isn't due to String. it is due to writeUTF(), which can only write 16 bits of length. – user207421 Dec 09 '12 at 11:36
  • http://stackoverflow.com/questions/13785594/writing-decoded-base64-byte-array-as-image-file/13785647#13785647 the code is hint,there is noting mentioned like taking file from android folder.I am asuming you can do it you self ..the code is telling you how I am encoing that file into string (first part of code) and second part of code shows that how i am converting string back into image on server side – Amir Qayyum Khan Dec 09 '12 at 11:38

1 Answers1

1

@Sarram follow the code in the blow link, I was sending images in soap request along with other data in the form of base64String the i was converting it into file

blow is the reference of code

Writing decoded base64 byte array as image file

I am using this cool decoder import sun.misc.BASE64Decoder; Server side can do it like that

        String filePath = "/destination/temp/file_name.jpg";
        File imageFile = new File(filePath);
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(imageFile);//create file
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        BASE64Decoder decoder = new BASE64Decoder();//create decodeer object
        byte[] decodedBytes = null;
        try {
            decodedBytes = decoder.decodeBuffer(imageFileBase64);//decode base64 string that you are sending from clinet side 
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        try {
            fos.write(decodedBytes);//write the decoded string on file and you have ur image at server side
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
Community
  • 1
  • 1
Amir Qayyum Khan
  • 473
  • 2
  • 15
  • were you retrieving images from sd card of android emulator ? – Saaram Dec 09 '12 at 11:29
  • here i am sending images from client to server so i need to encode it on android and decode it on the server in java – Saaram Dec 09 '12 at 11:32
  • I was taking input an image from SD card in android clinet and then i was encoding it into base64 String and the i was puting that string into soap call request and the on server side i was decoding it, the code for decoding image in the for of base 64 String and converting it into file is above – Amir Qayyum Khan Dec 09 '12 at 11:33