Android使用PrinterShare实现蓝牙打印

本文介绍如何在Android应用中打印检查记录表,并使用PrinterShare软件进行打印。通过复制模板文件,替换内容,最终实现文档的生成与打印。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需要的类包为:poi-3.8-beta4-20110826.jar poi-scratchpad-3.8-beta4-20110826.jar
代码如下:

package com.example.printer;




import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;


import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


/**
 * @time 2015年12月14日08:28:28
 * @author osy 
 * @version 1.0
 */
public class PrinterActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.printer);
Button prinrerBtn=(Button)findViewById(R.id.button1);
prinrerBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
printer();

}
});
}


//打印检查记录表
/**
 * 为了保证模板的可用,最好在现有的模板上复制后修改
 */
private void printer(){
try {
saveFile("xcjcjl.doc", PrinterActivity.this, R.raw.xcjcjl);//文件目录res/raw
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//现场检查记录
String aafileurl=Environment.getExternalStorageDirectory()+"/inspection/xcjcjl.doc";
final String bbfileurl=Environment.getExternalStorageDirectory()+"/inspection/xcjcjl_printer.doc";
//获取模板文件
File demoFile=new File(aafileurl);
//创建生成的文件
File newFile=new File(bbfileurl);
if(newFile.exists()){
newFile.delete();
}
Map<String, String> map = new HashMap<String, String>();
map.put("$record_companyName$", "涉及项目不提供");
map.put("$record_companyAddress$", "涉及项目不提供");
map.put("$record_companyPic$", "涉及项目不提供");
map.put("$record_companyWork$", "涉及项目不提供");
map.put("$record_companyPhone$","涉及项目不提供");
map.put("$record_CheckAddress$", "涉及项目不提供");
map.put("$time_nian$", "涉及项目不提供");
map.put("$time_yue$", "涉及项目不提供");
map.put("$time_ri$", "涉及项目不提供");
map.put("$time_shi$", "涉及项目不提供");
map.put("$time_fen$", "涉及项目不提供");
map.put("$time_ri2$", "涉及项目不提供");
map.put("$time_shi2$", "涉及项目不提供");
map.put("$time_fen2$", "涉及项目不提供");
map.put("$record_jcjg$", "涉及项目不提供");
map.put("$record_userName$", "涉及项目不提供");
map.put("$record_userName2$", "涉及项目不提供");
map.put("$record_userNum$", "涉及项目不提供");
map.put("$record_userNum2$", "涉及项目不提供");
map.put("$content$", "涉及项目不提供");
if(writeDoc(demoFile,newFile,map)){

//调用printershare软件来打印该文件
File picture = new File(bbfileurl);
Uri data_uri=Uri.fromFile(picture);
/* data_type - Mime type. MIME类型如下:


"application/pdf"
"text/html"
"text/plain"
"image/png"
"image/jpeg"
"application/msword" - .doc
"application/vnd.openxmlformats-officedocument.wordprocessingml.document" - .docx
"application/vnd.ms-excel" - .xls
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" - .xlsx
"application/vnd.ms-powerpoint" - .ppt
"application/vnd.openxmlformats-officedocument.presentationml.presentation" - .pptx
*/
try {

String data_type="application/msword";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setPackage("com.dynamixsoftware.printershare.amazon");//未注册之前com.dynamixsoftware.printershare,注册后加上amazon
i.setDataAndType(data_uri, data_type);
startActivity(i);
} catch (Exception e) {
//没有找到printershare
}

}


}
/**
 * demoFile 模板文件
 * newFile 生成文件
 * map 要填充的数据
 * */
public boolean writeDoc(File demoFile ,File newFile ,Map<String, String> map)
{
try
{ 
FileInputStream in = new FileInputStream(demoFile);
HWPFDocument hdt = new HWPFDocument(in);
// Fields fields = hdt.getFields();
// 读取word文本内容
Range range = hdt.getRange();
// System.out.println(range.text());

// 替换文本内容
for(Map.Entry<String, String> entry : map.entrySet())
{
range.replaceText(entry.getKey(), entry.getValue());
}
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
FileOutputStream out = new FileOutputStream(newFile, true);
hdt.write(ostream);
// 输出字节流
out.write(ostream.toByteArray());
out.close();
ostream.close();
return true;
}
catch(IOException e)
{
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
return false;
}


/**
 * 将文件复制到SD卡,并返回该文件对应的数据库对象
 * 
 * @return
 * @throws IOException
 */
public void saveFile(String fileName, Context context, int rawid) throws IOException {


// 首先判断该目录下的文件夹是否存在
File dir = new File(Environment.getExternalStorageDirectory() + "/inspection/" );
if (!dir.exists()) {
// 文件夹不存在 , 则创建文件夹
dir.mkdirs();
}


// 判断目标文件是否存在
File file1 = new File(dir, fileName);


if (!file1.exists()) {
file1.createNewFile(); // 创建文件


}
// 开始进行文件的复制
InputStream input = context.getResources().openRawResource(rawid); // 获取资源文件raw
// 标号
try {


FileOutputStream out = new FileOutputStream(file1); // 文件输出流、用于将文件写到SD卡中
// -- 从内存出去
byte[] buffer = new byte[1024];
int len = 0;
while ((len = (input.read(buffer))) != -1) { // 读取文件,-- 进到内存


out.write(buffer, 0, len); // 写入数据 ,-- 从内存出
}


input.close();
out.close(); // 关闭流
} catch (Exception e) {


e.printStackTrace();
}


}
}

模板为:保存格式为doc

PrinterShare的MIME:
"application/pdf"
"text/html"
"text/plain"
"image/png"
"image/jpeg"
"application/msword" - .doc
"application/vnd.openxmlformats-officedocument.wordprocessingml.document" - .docx
"application/vnd.ms-excel" - .xls
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" - .xlsx
"application/vnd.ms-powerpoint" - .ppt
"application/vnd.openxmlformats-officedocument.presentationml.presentation" - .pptx

PrinterShare注意事项:

Version: 11.0.1
Release date: 2015-12-11
如果没有注册的,请用

try {

String data_type="application/msword";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setPackage("com.dynamixsoftware.printershare“);//测试版
i.setDataAndType(data_uri, data_type);
startActivity(i);
} catch (Exception e) {
//没有找到printershare
}

注册后:

try {

String data_type="application/msword";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setPackage("com.dynamixsoftware.printershare.amazon");//注册版
i.setDataAndType(data_uri, data_type);
startActivity(i);
} catch (Exception e) {
//没有找到printershare
}

亲测成功!新手求积分!高手勿喷!
资源下载地址:PrinterShare实现蓝牙上传-Android

产品特色: PrinterShare移动打印直接打印手机上的文件、包括来自SD 卡及Google 文件、Gmail、相片、联络资料、行事历、SMS/MMS、通话记录及网页的文件(DOC、DOCX、XLS、XSLX、PPT、 PPTX、PDF、TXT)。 功能: *经由Wi-Fi 或蓝牙,即可在附近的印表机直接列印,无须使用电脑; *列印至附近PC 或Mac 共享的印表机; *无限次远端列印。接收端(Mac 或PC) 无须购买页数或订用服务。 近端列印:如果您有Wi-Fi 或蓝牙印表机,就可直接在该印表机上列印。在主选单中选择附近的印表机,然后选择想要使用的印表机即可。如果您的印表机并不支援此功能,Windows 用户可到printershare.com 下载我们免费提供的软件,并在安装软件后共用您的印表机。 Mac 用户则可使用内建的Mac OS X Printer Sharing 服务来共享印表机。近端列印服务需使用PrinterShare Premium Key。您可在Market 购买Premium Key,或者到printershare.com 以PayPal 付款购买。购买Premium Key 之前,我们强烈建议您首先列印测试页面。 远端列印:您可经由互联网,在世上所有角落进行列印,包括您自己的印表机。接收列印要求的一端,无论使用Mac 还是PC,均须进入printershare.com 取得我们的免费软件并加以安装,才能共享印表机。您在装置里找到共享印表机后,就能列印。远端列印须使用PrinterShare 用户帐户。如果电脑和装置都由您控制,我们建议两者均使用相同的帐户注册。使用Premium Key 后,远端列印的限制就会取消。否则,接收列印要求的一方,就须购买页数或订用付费服务。首 20 页费用全免。 云端列印:PrinterShare 支援Google 云端列印,无须另行收费。要执行云端列印,您的电脑上需要安装最新版本的Google Chrome 浏览器。如欲取得更多设定指示,请参阅google.com/support/cloudprint 从装置上列印时,我们一般建议使用Wi-Fi 或3/4G,以获得更佳效能。但是,EDGE/GPRS 或任何其他连接方式也能使用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值