Android使用PrinterShare实现蓝牙打印
2015-12-14 13:36
761 查看
需要的类包为:poi-3.8-beta4-20110826.jar poi-scratchpad-3.8-beta4-20110826.jar
代码如下:
模板为:保存格式为doc
Release date: 2015-12-11
如果没有注册的,请用
注册后:
亲测成功!新手求积分!高手勿喷!
资源下载地址:PrinterShare实现蓝牙上传-Android
代码如下:
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.1Release 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
相关文章推荐
- 安卓手机连接蓝牙打印机实现打印功能
- iOS Bluetooth 打印小票(二)
- android 蓝牙打印
- 佳博打印机android版本代码优化
- Android pad 连接蓝牙打印机Gprinter---实现蓝牙打印功能
- android 自动生成ldpi, mdpi, hdpi, xhdpi, xxhdpi, xxxhdpi图片的工具
- Android Context完全解析,你所不知道的Context的各种细节
- Android消息机制源码解析(二)——消息的执行者Handler
- Android 欢迎页TimerTask的使用,及注意事项
- 每个Android开发者必须知道的内存管理知识
- 强大的Android参数模拟器,自由修改手机型号、SDK版本号等信息
- android Xutile ,Voller 对比
- android四大组件
- Android如何获得手机power_profile.xml文件
- Android 控件之RatingBar评分条(五星)自定义样式
- android studio 如何修改包名
- Android Studio 常用快捷键
- Android开源框架
- android加混淆 方式与坑