Printing in android only searching the printers -


i have made android app in have tried print sample text file,i want use wifi connected printers,i tried linkwifi printing in android goes searching wifi printer , nothing,my code below,please me , save life

code

public class mainactivity extends activity {      public int pageheight;     public int pagewidth;     public pdfdocument mypdfdocument;     public int totalpages = 4;     button button1;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.main);         button1 = (button) findviewbyid(r.id.button1);      }      printdocumentadapter pda = new printdocumentadapter() {          @override         public void onwrite(pagerange[] pages,                 parcelfiledescriptor destination,                 cancellationsignal cancellationsignal,                 writeresultcallback callback) {             inputstream input = null;             outputstream output = null;              try {                  input = getassets().open("sample.txt");                 output = new fileoutputstream(destination.getfiledescriptor());                  byte[] buf = new byte[1024];                 int bytesread;                  while ((bytesread = input.read(buf)) > 0) {                     output.write(buf, 0, bytesread);                 }                  callback.onwritefinished(new pagerange[] { pagerange.all_pages });              } catch (filenotfoundexception ee) {                 // catch exception             } catch (exception e) {                 // catch exception             } {                 try {                     input.close();                     output.close();                 } catch (ioexception e) {                     e.printstacktrace();                 }             }         }          @suppresslint("inlinedapi")         @override         public void onlayout(printattributes oldattributes,                 printattributes newattributes,                 cancellationsignal cancellationsignal,                 layoutresultcallback callback, bundle extras) {              if (cancellationsignal.iscanceled()) {                 callback.onlayoutcancelled();                 return;             }              //int pages = computepagecount(newattributes);              printdocumentinfo pdi = new printdocumentinfo.builder(                     "name of file").setcontenttype(                     printdocumentinfo.content_type_document).build();              callback.onlayoutfinished(pdi, true);         }     };      @suppresslint("inlinedapi")     public void printdocument(view view) {         printmanager printmanager = (printmanager)                 .getsystemservice(context.print_service);         string jobname = this.getstring(r.string.app_name) + " document";         printmanager.print(jobname, pda, null);     }  } 

manifest

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.example.wifiprint"     android:versioncode="1"     android:versionname="1.0" >      <uses-sdk         android:minsdkversion="8"         android:targetsdkversion="21" />      <uses-feature android:name="android.hardware.wifi" />      <uses-permission android:name="android.permission.access_wifi_state" />     <uses-permission android:name="android.permission.access_network_state" />     <uses-permission android:name="android.permission.change_wifi_state" />     <uses-permission android:name="android.permission.write_external_storage" />     <uses-permission android:name="android.permission.read_external_storage" />      <application         android:allowbackup="true"         android:icon="@drawable/ic_launcher"         android:label="@string/app_name"         android:theme="@style/apptheme" >         <activity             android:name=".mainactivity"             android:label="@string/app_name" >             <intent-filter>                 <action android:name="android.intent.action.main" />                  <category android:name="android.intent.category.launcher" />             </intent-filter>         </activity>     </application>  </manifest> 

you should make button listener first button event.

    button button1 = (button) findviewbyid(r.id.button1);     button1.setonclicklistener(new view.onclicklistener() {          @override         public void onclick(view arg0) {             // todo auto-generated method stub             docustomprint();         }     }); 

and print method custom document print;

    private void docustomprint() {      printdocumentadapter pda = new printdocumentadapter(){          @override         public void onwrite(pagerange[] pages, parcelfiledescriptor destination, cancellationsignal cancellationsignal, writeresultcallback callback){             inputstream input = null;             outputstream output = null;              assetmanager assetmanager = getassets();              try {                 // file print.                 input = assetmanager.open("abc.pdf");                 output = new fileoutputstream(destination.getfiledescriptor());                  byte[] buf = new byte[1024];                 int bytesread;                  while ((bytesread = input.read(buf)) > 0) {                      output.write(buf, 0, bytesread);                 }                  callback.onwritefinished(new pagerange[]{pagerange.all_pages});              } catch (filenotfoundexception ee){                 //catch exception             } catch (exception e) {                 //catch exception             } {                 try {                     input.close();                     output.close();                 } catch (ioexception e) {                     e.printstacktrace();                 }             }         }          @override         public void onlayout(printattributes oldattributes, printattributes newattributes, cancellationsignal cancellationsignal, layoutresultcallback callback, bundle extras){              if (cancellationsignal.iscanceled()) {                 callback.onlayoutcancelled();                 return;             }              printdocumentinfo pdi = new printdocumentinfo.builder("print_doc.pdf").setcontenttype(printdocumentinfo.content_type_document).build();             callback.onlayoutfinished(pdi, true);         }     };      printmanager printmanager = (printmanager) this.getsystemservice(context.print_service);     string jobname = this.getstring(r.string.app_name) + " document";     printmanager.print(jobname, pda, null); } 

and check documents.

http://developer.android.com/training/printing/index.html

how print pdf using android 4.4 printing framework


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -