How to print data in a new line and set page layouts for printing data from android -
i creating android application consists of printing data printer connected via usb. here, want set page layouts such new line printing when reached @ end of page , page borders etc. possible via programatically. please me how this.
this had done printing data:
public class demoprinter extends activity { private final string action_usb_permission = "com.android.example.usb_permission"; button create_file; pendingintent mpermissionintent; usbmanager usbmanager; usbdevice device; edittext print_text; usbdevice printer = null; private static final int printer_vendor_id = 1256; private string filename = "mysamplefile.txt"; private string filepath = "panelfilesstorage"; file myinternalfile; string data= "this sample text"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); try { setcontentview(r.layout.activity_demo_printer); contextwrapper contextwrapper = new contextwrapper(getapplicationcontext()); file directory = contextwrapper.getdir(filepath, context.mode_private); myinternalfile = new file(directory , filename); print_text = (edittext)findviewbyid(r.id.edittext_printer); usbmanager = (usbmanager) getsystemservice(context.usb_service); hashmap<string, usbdevice> devicelist = usbmanager.getdevicelist(); if (devicelist.size() <= 0) { toast.maketext(getapplicationcontext(), "info: no device found", toast.length_short).show(); } else { toast.maketext(getapplicationcontext(), "no of devices"+devicelist.size(), toast.length_short).show(); ((textview) findviewbyid(r.id.textview_devices)) .settext("no of device : " + devicelist.size()); } iterator<usbdevice> deviceiterator = devicelist.values().iterator(); int count = 0; mpermissionintent = pendingintent.getbroadcast(getbasecontext(), 0, new intent(action_usb_permission), 0); while (deviceiterator.hasnext()) { count++; device = deviceiterator.next(); log.i("info", "device no " + count + "........"); log.i("info", "vendor id : " + device.getvendorid()); log.i("info", "product id : " + device.getproductid()); log.i("info", "device name : " + device.getdevicename()); log.i("info", "device class : " + device.getclass().getname()); log.i("info", "device protocol: " + device.getdeviceprotocol()); log.i("info", "device subclass : " + device.getdevicesubclass()); if (device.getvendorid() == printer_vendor_id) { printer = device; break; } } findviewbyid(r.id.button_print).setonclicklistener( new onclicklistener() { @override public void onclick(view v) { log.i("info", "print command given"); intentfilter filter = new intentfilter( action_usb_permission); registerreceiver(musbreceiver, filter); if (printer != null) { usbmanager.requestpermission(printer, mpermissionintent); } else { log.e("exception", "printer not found"); } } }); } catch (exception e) { log.e("exception", "exception in oncreate " + e.getmessage()); e.printstacktrace(); } findviewbyid(r.id.button_create_file).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { try { fileoutputstream fos = new fileoutputstream(myinternalfile+"\n"); fos.write(data.tostring().getbytes()); fos.close(); } catch (ioexception e) { e.printstacktrace(); } } }); } private final broadcastreceiver musbreceiver = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { try { string action = intent.getaction(); if (action_usb_permission.equals(action)) { synchronized (this) { final usbdevice printerdevice = (usbdevice) intent .getparcelableextra(usbmanager.extra_device); if (intent.getbooleanextra( usbmanager.extra_permission_granted, false)) { if (printerdevice != null) { log.i("info", "device permission granted"); startprinting(printerdevice); } } else { log.d("debug", "permission denied device " + printerdevice); } } } } catch (exception e) { log.e("exception", "exception in onrecieve " + e.getmessage()); e.printstacktrace(); } } }; public void startprinting(final usbdevice printerdevice) { new handler().post(new runnable() { usbdeviceconnection conn; usbinterface usbinterface; @override public void run() { try { log.i("info", "bulk transfer started"); usbinterface = printerdevice.getinterface(0); usbendpoint endpoint = usbinterface.getendpoint(0); conn = usbmanager.opendevice(printer); conn.claiminterface(usbinterface, true); string mystringdata = print_text.gettext().tostring(); mystringdata+="\n"; byte[] array = mystringdata.getbytes(); bytebuffer output_buffer = bytebuffer .allocate(array.length); usbrequest request = new usbrequest(); request.initialize(conn, endpoint); request.queue(output_buffer, array.length); if (conn.requestwait() == request) { log.i("info", output_buffer.getchar(0) + ""); message m = new message(); m.obj = output_buffer.array(); // handler.sendmessage(m); output_buffer.clear(); } else { log.i("info", "no request recieved"); } int transfered = conn.bulktransfer(endpoint, mystringdata.getbytes(), mystringdata.getbytes().length, 5000); log.i("info", "amount of data transferred : " +transfered); } catch (exception e) { log.e("exception", "unable transfer bulk data"); e.printstacktrace(); } { try { conn.releaseinterface(usbinterface); log.i("info", "interface released"); conn.close(); log.i("info", "usb connection closed"); unregisterreceiver(musbreceiver); log.i("info", "brodcast reciever unregistered"); } catch (exception e) { log.e("exception", "unable release resources because : " + e.getmessage()); e.printstacktrace(); } } } }); } }
Comments
Post a Comment