1

I need to develop an application to transfer image continuously from android to android which should be received as an video on the client side..and i have two applications which one (server) will send the file through socket connected by wifi and other should the client side should recive the image which is sent...currently am just saving it in one location in the client side...am able to receive the file correctly...but the problem is am not able to send all files correctly all the time... Means some time the image file will be transfered and some time i ll not be able to receive and when i ll not be able to receive i am getting an exception as : java.io.UTFDataFormatException: ...and the file is not written and saved on the receiving side...

If am not able to receive images continuously...i can think there is some problem in the code..but am able to transfer it some times..and some time not able to transfer...am not able to figure what the issue is...plz any guidance

the error is:

11-18 10:38:17.351: W/System.err(1001): java.io.UTFDataFormatException: bad second or third byte at 2 11-18 10:38:17.359: W/System.err(1001): at java.nio.charset.ModifiedUtf8.decode(ModifiedUtf8.java:53) 11-18 10:38:17.359: W/System.err(1001): at java.io.DataInputStream.decodeUTF(DataInputStream.java:444) 11-18 10:38:17.359: W/System.err(1001): at java.io.DataInputStream.decodeUTF(DataInputStream.java:438) 11-18 10:38:17.359: W/System.err(1001): at java.io.DataInputStream.readUTF(DataInputStream.java:433)

and the file is not saved when i get this exception..

Many scenarios i have tested by capturing image and saving and sending...and also compressing the image and sending...in these scenarios some very rarely it is going....am not able to figure out it...

Sender code: File myFile = new File(sdCard+"/image/image.jpg");
byte[] mybytearray = new byte[(int) myFile.length()];

           FileInputStream fis = new FileInputStream(myFile);  
           BufferedInputStream bis = new BufferedInputStream(fis);  
           //bis.read(mybytearray, 0, mybytearray.length);  

           DataInputStream dis = new DataInputStream(bis);     
           dis.readFully(mybytearray, 0, mybytearray.length);  

           OutputStream os = socket.getOutputStream();  
           tv.setText("Send file name size to server");

           //Sending file name,file size and to the server  
           DataOutputStream dos = new DataOutputStream(os);     
           dos.writeUTF(myFile.getName());     
           dos.writeLong(mybytearray.length);     
           dos.write(mybytearray, 0, mybytearray.length);     
           dos.flush();  
           socket.close();
           tv.setText("Socket Close");

           tv.setText("Sent");

Receiver Code: File dir = new File (sdCard.getAbsolutePath() + "/dir1/dir2"); dir.mkdirs();

            String fileName = clientData.readUTF();   
            File file = new File(dir,fileName);

            OutputStream output = new FileOutputStream(file);     
            long size = clientData.readLong();     
            byte[] buffer = new byte[1024];     
            while (size > 0 && (bytesRead = clientData.read(buffer, 0, (int)Math.min(buffer.length, size))) != -1)     
            {     
                output.write(buffer, 0, bytesRead);     
                size -= bytesRead; 
                System.out.println("Writing");
            }  
             // status.setText("Received");
            // Closing the FileOutputStream handle  
            output.close();
            s.close();

Thanks and Regards, Divya.K

3
  • Apart from your bug, you will not be able to stream video in any kind of sane manner if you continually open and close sockets. The latency will be just hopeless. Nov 18, 2013 at 12:57
  • Hiii Martin James,Thanks for your reply..then apart from this what is the solution for the problem means streaming video from android to android through sockets..coz previously i had tried using mediarecorder property as i hav posted in stackoverflow.com/questions/18886981/… ..finally i was able to save the video file recorded..but i was not able to play the video..the video was encoded..it was able to play using 3rd party media player like MX player..but not in default mediaplayer.so i started to implement like this Nov 19, 2013 at 4:49
  • I have tried many ways as i found in google but no proper solution..so plz can u guide me how to stream online video from android to android through wifi via sockets...any ideas..and i didnt try to implement like Spydroid IP camera by creating RTP packets and al..i need to implement without any intermediate server..just through sockets..direct streaming...so how can i proceed to implement... Nov 19, 2013 at 4:59

1 Answer 1

0

I was stuck on this thing i combined few days of my search and reached to this... Try it and see if works for you..

  Bitmap myBitmap = null;
        try {
            File imgFile = new File(imgPath);
            if (imgFile.exists()) {

                File image = new File(imgPath, "imagename.jpg");
                BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                myBitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
                //myBitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true); //uncomment this if you want to scale the image
                //imageView.setImageBitmap(bitmap);//to set bitmap to image view
            }

            Socket sock = new Socket("192.168.1.1", 80);//ip adress and port number
                 ByteArrayOutputStream bos = new ByteArrayOutputStream();
            if (myBitmap != null) {
                myBitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
            }
            byte[] bitmapdata = bos.toByteArray();
            ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);


            BufferedInputStream bis = new BufferedInputStream(bs);
            bis.read(bitmapdata, 0, bitmapdata.length);
            OutputStream os = sock.getOutputStream();
            System.out.println("Sending...");
            os.write(bitmapdata, 0, bitmapdata.length);
            os.flush();
            sock.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.