android - 在android中,如何在没有打印机插件的情况下打印

标签 android printing

在android中,我们可以在应用程序内存中添加打印机插件或者在 Assets 文件夹中添加jar文件,并尝试从应用程序中调用它,以创建移动打印。

据我所知

仅限

>打印共享应用程序选项。没有谷歌云或任何云打印的任何其他方式。

最佳答案

This is working for me,I am passing pdf file to printer it is printing!!

class WifiPrint extends Activity { TextView printer_name, gate_way, printer_port; int port = 9100; WifiManager wifi = null; android.net.DhcpInfo d; String gateway_ip = "";

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_wifi_print); wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); printer_name = (TextView) findViewById(R.id.textView1); gate_way = (TextView) findViewById(R.id.textView2); printer_port = (TextView) findViewById(R.id.textView3); /* de.lohndirekt.print.IppPrintService svc = new IppPrintService(printerURI); InputStream stream = new BufferedInputStream(new FileInputStream("image.epl")); DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; Doc myDoc = new SimpleDoc(stream, flavor, null); DocPrintJob job = svc.createPrintJob(); job.print(myDoc, null);*/

}

@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.wifi_print, menu); return true; }

public void wifisettings(View v) { startActivityForResult(new Intent( android.provider.Settings.ACTION_WIFI_SETTINGS), 0); }

public void connectSocket(View v) {
/*Uri filepath=Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.pdfsample); Toast.makeText(this, filepath.getPath(), Toast.LENGTH_LONG).show(); */ ClientThread clientThread = new ClientThread(""); Thread clientstartThread = new Thread(clientThread); clientstartThread.start(); }

public String intToIp(int i) { return ((i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF) + "." + ((i >> 24) & 0xFF)); }

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub

  super.onActivityResult(requestCode, resultCode, data);      if

(requestCode == 0) { WifiInfo wifiinfo = wifi.getConnectionInfo(); d = wifi.getDhcpInfo();

      printer_name.setText("Name:" + wifiinfo.getSSID());             gateway_ip =

intToIp(d.gateway); gate_way.setText("Printer Ip:" + gateway_ip); printer_port.setText("Port:" + port);

  }   }

public File createFileFromInputStream(InputStream inputStream) {

 try{
    File f = new File("/sdcard/testpdf.pdf");
    OutputStream outputStream = new FileOutputStream(f);
    byte buffer[] = new byte[1024];
    int length = 0;

    while((length=inputStream.read(buffer)) > 0) {
      outputStream.write(buffer,0,length);
    }

    outputStream.close();
    inputStream.close();

    return f;
 }catch (IOException e) {
       //Logging exception
 }

 return null;     }

class ClientThread implements Runnable { String filepath = ""; BufferedInputStream bis = null; FileInputStream fis;

  Socket socket;

  public ClientThread(String filename) {          filepath = filename;        }

  @Override       public void run() {             // TODO Auto-generated method

stub try {
InputStream in = getResources().openRawResource(R.raw.pdfsample); createFileFromInputStream(in); File pdfFile = new File("/sdcard/testpdf.pdf"); byte[] mybytearray = new byte[(int) pdfFile.length()]; socket = new Socket(gateway_ip, port); runOnUiThread(new Runnable() {
@Override public void run() { // TODO Auto-generated method stub if(socket.isConnected()){ Toast.makeText(WifiPrint.this, "Socket Connected", Toast.LENGTH_LONG).show();
}
} });
fis = new FileInputStream(pdfFile); bis = new BufferedInputStream(fis); bis.read(mybytearray, 0, mybytearray.length); OutputStream os = socket.getOutputStream(); os.write(mybytearray, 0, mybytearray.length); os.flush();

          if (bis != null) {
              bis.close();
              os.close();
              socket.close();
              fis.close();

          }
              } catch (final UnknownHostException e) {
          // TODO Auto-generated catch block
          runOnUiThread(new Runnable() {

              @Override
              public void run() {
                  // TODO Auto-generated method stub
                  Toast.makeText(WifiPrint.this,
                          "UnHost Exceptions" + e.getMessage(),
                          Toast.LENGTH_LONG).show();

              }
          });             } catch (final IOException e) {
          // TODO Auto-generated catch block
          runOnUiThread(new Runnable() {

              @Override
              public void run() {
                  // TODO Auto-generated method stub
                  Toast.makeText(WifiPrint.this,
                          "Io Exceptions" + e.getMessage(),
                          Toast.LENGTH_LONG).show();

              }
          });             } finally {
          try {
              if (bis != null) {
                  bis.close();
                  socket.close();
                  fis.close();
              }
          } catch (final IOException e) {
              // TODO Auto-generated catch block
              runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
                      // TODO Auto-generated method stub
                      Toast.makeText(
                              WifiPrint.this,
                              "Io Exeption in Closing Socket"
                                      + e.getMessage(), Toast.LENGTH_LONG)
                              .show();
                  }
              });
          }           }       }

}

}

在 list 文件中:只需添加:

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

<uses-feature android:name="android.hardware.wifi" />

关于android - 在android中,如何在没有打印机插件的情况下打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34605641/

相关文章:

android - 从异步回调更新 Ionic View

java - 安卓 SQLite : Check if row exists in table

printing - Bootstrap 3页打印移动版

java - 有没有办法使用 ESC 命令获取热敏打印机的状态?

java - GPS 寻找高度

android - AngEngine 动态壁纸

android - 当用户点击系统通知时,如何在 Cordova+Ember 中加载自定义位置?

javascript - Web 应用程序中的打印机交互使用 javascript、jquery

python - python读取文件并打印出1、2、3、4字母的单词数

java - 打印整个可滚动 Swing 组件