Question

I have custom socket client server data (file or text) transmission code. Now when I transfer binary files, some bytes convert onto out of range characters. So I send them in hex string. That works. But for another problem this is not the solution. This has a performance problems as well.

I took help from Java code To convert byte to Hexadecimal.

When I download images from the net, same thing happens. Some bytes change into something else. I have compared bytes by bytes. Converting into String show ? instead of the symbol. I have tried readers and byte array input stream. I have tried all the examples on the net. What is the mistake I could be doing?

My Code to save bytes to file:

void saveFile(String strFileName){  


 try{  
         URL url = new URL(strImageRoot + strFileName);  
         BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));  
         BufferedWriter bw = new BufferedWriter(new FileWriter(strImageDownloadPath + strFileName));  
         String line = null;  
         while ( (line = reader.readLine()) != null) {  
            bw.write(line);  
         }  
     }catch(FileNotFoundException fnfe){  
        System.out.println("FileNotFoundException occured!!!");  
     }catch(IOException ioe){  
     }catch(Exception e){  
        System.out.println("Exception occured : " + e);  
     }finally{  
        System.out.println("Image downloaded!!!");  
     }  
}   
Was it helpful?

Solution 3

You should take the help of this link: How to encode decode in base64 in Android.

You can send byte array obtained from a file as string by encoding into Base64. This reduces the amount of data transmitted as well.

At the receiving end just decode the string using Base64 and obtain byte array.

Then you can use @Deepak Swami's solution to save bytes in file.

I recently found out that PHP service APIs do not know about what is byte array. Any String can be byte stream at the same time, so the APIs expect Base64 string in the request parameter. Please see the posts:

String to byte array in php

Passing base64 encoded strings in URL

Hence Base64 has quite importance as also it allows you to also save byte arrays in preferences, and increases performance if you have to send file data across network using Serialization.

Happy Coding :-)

OTHER TIPS

i had a similar issue when i was building a Socket client server application. The bytes would be some weird characters and i tried all sorts of things to try and compare them. Then i came across a discussion where some1 pointed out to me that i should use a datainputstream, dataoutstream and let that do the conversion to and from bytes. that worked for me totally. i never touched the bytes at all.

use this code

           File root = android.os.Environment.getExternalStorageDirectory();               

           File dir = new File (root.getAbsolutePath() + "/image");
           if(dir.exists()==false) {
                dir.mkdirs();
           }

           URL url = new URL("http://4.bp.blogspot.com/-zqJs1fVcfeY/TiZM7e-pFqI/AAAAAAAABjo/aKTtTDTCgKU/s1600/Final-Fantasy-X-Night-Sky-881.jpg");
           //URL url = new URL(DownloadUrl);
           //you can write here any link
           File file = new File(dir,"Final-Fantasy-X-Night-Sky-881.jpg");



           long startTime = System.currentTimeMillis();


            //Open a connection to that URL. 
           URLConnection ucon = url.openConnection();


            //* Define InputStreams to read from the URLConnection.

           InputStream is = ucon.getInputStream();
           BufferedInputStream bis = new BufferedInputStream(is);


            //* Read bytes to the Buffer until there is nothing more to read(-1).

           ByteArrayBuffer baf = new ByteArrayBuffer(6000);
           int current = 0;
           while ((current = bis.read()) != -1) {
              baf.append((byte) current);
           }


            //Convert the Bytes read to a String. 
           FileOutputStream fos = new FileOutputStream(file);
           fos.write(baf.toByteArray());
           fos.flush();
           fos.close();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top