2 Replies - 1711 Views - Last Post: 22 January 2013 - 09:49 PM

#1 Mahi07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-January 13

Send files to WiFi printer

Posted 21 January 2013 - 11:14 PM

I am developing an application in which from my mobile I am sending files to the WiFi printer by IP address and port number, which are .txt, .png, .jpg, .doc. They should be printed from the printer. I have tried following code but it is giving me output for .txt file only. What changes are required to get output of all type of files???

import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.net.Socket;
    import java.net.UnknownHostException;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;

    public class MainActivity extends Activity {

     private Socket client;
     private FileInputStream fileInputStream;
     private BufferedInputStream bufferedInputStream;
     private OutputStream outputStream;
     private Button button;
     private TextView text;
     private EditText etIp, etPort;
     int port=0;

     @Override
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      button = (Button) findViewById(R.id.button1);   //reference to the send button
      text = (TextView) findViewById(R.id.textView1);   //reference to the text view
      etIp = (EditText) findViewById(R.id.editText1);
      etPort = (EditText) findViewById(R.id.editText2);
      //Button press event listener
      button.setonclickListener(new View.onclickListener() {

       public void onclick(View v) {


         //create file instance
           port=Integer.parseInt(etPort.getText().toString());
           File file = new File("/mnt/sdcard/bluetooth/AnyFile.txt"); 
        try 
        {

         client = new Socket(etIp.getText().toString(), port);

         byte[] mybytearray = new byte[(int) file.length()]; //create a byte array to file

         fileInputStream = new FileInputStream(file);
         bufferedInputStream = new BufferedInputStream(fileInputStream);  

         bufferedInputStream.read(mybytearray, 0, mybytearray.length); //read the file

         outputStream = client.getOutputStream();

         outputStream.write(mybytearray, 0, mybytearray.length); //write file to the output stream byte by byte
         outputStream.flush();
         bufferedInputStream.close();
         outputStream.close();
         client.close();

         text.setText("File Sent");


        } catch (UnknownHostException e) {
         e.printStackTrace();
        } catch (IOException e) {
         e.printStackTrace();
        }


       }
      });

     }
    }




Manifest file

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>

This post has been edited by Atli: 22 January 2013 - 12:32 AM
Reason for edit:: Use [code] tags when posting code.


Is This A Good Question/Topic? 0
  • +

Replies To: Send files to WiFi printer

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10807
  • View blog
  • Posts: 40,287
  • Joined: 27-December 08

Re: Send files to WiFi printer

Posted 22 January 2013 - 09:31 PM

Are you encountering any errors when trying to send a non-text file? Do you get a stack trace?
Was This Post Helpful? 0
  • +
  • -

#3 Mahi07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-January 13

Re: Send files to WiFi printer

Posted 22 January 2013 - 09:49 PM

View Postmacosxnerd101, on 22 January 2013 - 09:31 PM, said:

Are you encountering any errors when trying to send a non-text file? Do you get a stack trace?

No I am not getting any errors but also I am not getting any output...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1