Hi to all,
my app's users are asking a direct print feature in order to print a txt that generate my app.
I think that this feature could be very interesting for many apps.
I have found this document that talk about official "Android Printing Framework" :
More in detail I think there are two interesting section :
Creating and Printing HTML Content (Easy)
To enable HTML printing, the WebView class has been extended in Android 4.4 to include support for printing with minimal coding requirements.
Printing a Custom Document (more complicated)
@Erel it is possible to integrate this feature within B4A (wrapper library?)
If is not possible, can we use the "Simple Library Compiler" to use the java code show in the attached article ?
P.S.
For the Web printing method the sample java code is the following :
It should be quite easy to create a library for this code. You can also use JavaObject to run this code (though it is a bit more cumbersome). It will only work with Android 4.4+.
It should be quite easy to create a library for this code. You can also use JavaObject to run this code (though it is a bit more cumbersome). It will only work with Android 4.4+.
I'm trying to arrange the code using JavaObject (but with several doubt
B4X:
sub createWebPrintJob (webView as object)
Dim jPrintManager As JavaObject:jPrintManager.InitializeStatic("android.print.PrintManager")
Dim jPrintService As JavaObject = jPrintManager.RunMethod("getSystemService", Context.PRINT_SERVICE)
Dim jPrintAdapter As JavaObject = webView.RunMethod("createPrintDocumentAdapter", null)
dim jobName as String = getString(R.string.app_name) + " Document"; ' ?
dim printJob as JavaObject:jPrintManager.RunMethod("print", jobName)
PrintJob printJob = printManager.print(jobName, jPrintAdapter, new PrintAttributes.Builder().build());
end sub
@Erel Please, could you help me to write the code (is the first time that I'm trying a porting from java to B4A with JavaObject)
Sub CreateWebPrintJob (wv As WebView)
DIm jwv As JavaObject = wv
Dim jPrintManager As JavaObject = jwv.RunMethodJO("getContext", null).RunMethod("getSystemService", Array As Object("print"))
Dim jPrintAdapter As JavaObject = jwv .RunMethod("createPrintDocumentAdapter", null)
dim jobName as String = "Document"
jPrintManager.RunMethod("print", Array As Object(jobName, jPrintAdapter, null))
End Sub
Sub CreateWebPrintJob (wv As WebView)
DIm jwv As JavaObject = wv
Dim jPrintManager As JavaObject = jwv.RunMethodJO("getContext", null).RunMethod("getSystemService", Array As Object("print"))
Dim jPrintAdapter As JavaObject = jwv .RunMethod("createPrintDocumentAdapter", null)
dim jobName as String = "Document"
jPrintManager.RunMethod("print", Array As Object(jobName, jPrintAdapter, null))
End Sub
@Erel I confirm you that it works! Thanks!
Is it possible to configure the print service in order to get the print parameters (ex. default printer and so on..) in background without show the UI to the user ?
Just one question about that code posted above, using a webview works like a charm, to send a file (jpg, pdf, etc, etc) I believe it takes a InputStream, so, can that code be modified to do that?, Java is not my forte.
I rather wait for you too see if there's a simple solution, I believe there is, the CloudPrint app takes an input stream to send files, and this is the same just integrated into the OS.
@Erel I confirm you that it works! Thanks!
Is it possible to configure the print service in order to get the print parameters (ex. default printer and so on..) in background without show the UI to the user ?
The Good:
I'm testing the code with a sample app (using a webview) and it works fine (handling the above code within Main Activity).
The Bad:
I'm integrating the tested code within an existing app but within an activity different from the Main Activity.
The print UI window appear but than is closed automatically without let the user to start the print job and the activity is closed and the activity_resume event of the main activity is raised.
I'm investigating about this issue.
Do you confirm that calling the code within a different activity instead of Main activity the issue is confirmed?
@Erel in order to let print also previous version of android, it's possible to use Google cloud print ?
UPDATE:
I found a Google document that explain the steps to integrate the Google Cloud Print for run the print feature on Android 4.3 and below.
In few words, the solution proposed by Google need two files : print_dialog.xml and PrintDialogActivity.java
The UI is raised using an intent.
@Erel I implemented this feature (Android 4.4 print framework) with satisfaction in my app.
Only a doubt. There is a way to set the print on both sides of the paper ?