概述
我已经做了一个android应用程序,我试图打印一个示例文本文件,我想使用wifi连接打印机,我试过这个链接Wifi printing in android但它只是去搜索wifi打印机和什么都不做,我的代码如下,请帮助我,挽救我的生命在android中打印只搜索打印机
代码
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
} finally {
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) this
.getSystemService(Context.PRINT_SERVICE);
String jobName = this.getString(R.string.app_name) + " Document";
printManager.print(jobName, pda, null);
}
}
清单
package="com.example.wifiprint"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="21" />
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name=".MainActivity"
android:label="@string/app_name" >
+0
是您解决您的问题同样的问题对我来说。 –
最后
以上就是大气蜡烛为你收集整理的android 打印机列表中,在android中打印只搜索打印机的全部内容,希望文章能够帮你解决android 打印机列表中,在android中打印只搜索打印机所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复