Android使用PrinterShare实现蓝牙打印
来源:互联网 发布:mac chown命令详解 编辑:程序博客网 时间:2024/06/06 09:21
需要的类包为: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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.printer);Button prinrerBtn=(Button)findViewById(R.id.button1);prinrerBtn.setOnClickListener(new OnClickListener() {@Overridepublic 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 blocke.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,注册后加上amazoni.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
0 0
- Android使用PrinterShare实现蓝牙打印
- 在android中使用PrinterShare实现蓝牙打印功能
- android中使用PrinterShare实现蓝牙打印功能
- android文件打印的两种方法(外部app-printerShare-打印和蓝牙打印)
- android连接蓝牙打印机实现打印功能
- Android pad 连接蓝牙打印机Gprinter---实现蓝牙打印功能
- printershare打印excel中的遇到的几个问题
- 实现wm蓝牙打印
- Android蓝牙打印源码
- android蓝牙打印
- android 蓝牙打印
- android 蓝牙打印
- Android打印机--蓝牙打印
- Android蓝牙连接打印
- Android连接蓝牙打印机实现PDF文档的打印
- android通过蓝牙连接打印机实现格式化打印
- android通过蓝牙连接打印机实现格式化打印(二)
- Android蓝牙相关—蓝牙打印
- Java线程面试题 Top 50
- android 自动生成ldpi, mdpi, hdpi, xhdpi, xxhdpi, xxxhdpi图片的工具
- 传智播客助你尽快走出大学毕业后的迷茫
- docker basic guide
- 先安装Linux再安装windows双系统引导项处理
- Android使用PrinterShare实现蓝牙打印
- Activity生命周期一般解析
- [POJ 2411] Mondriaan's Dream
- 51单片机复位(高电平复位)
- spring框架(三)junit单元测试
- POJ-1860 Currency Exchange
- Labview的文件路径:当前VI路径和应用程序目录的区别
- [笔记] 大型网站技术架构——核心原理与案例分析 [十]
- Android提供的LruCache类简介