JiaYu_Guo 2018-02-24 02:33 采纳率: 33.3%
浏览 1081
已结题

安卓打印队列提取问题

代码如下,先获取一个打印管理器,再取得打印队列第一项,将内容输出,为什么运行不起来图片说明

或者有编写插件提取安卓打印文件的方法也可以

  • 写回答

2条回答 默认 最新

  • lemonCase 2018-02-24 07:43
    关注

    public class PrintUtil {

    private static final String TAG = "PrintUtil";

    public String SCREEN_SHOTS_LOCATION = Environment

    .getExternalStorageDirectory().getPath();

    /**

    • 对当前view进行截屏,并保存到SCREEN_SHOTS_LOCATION指定的目录下
    • @param view
    • @param name
    • @return
    • @throws Exception
      */

      public void takeScreenShot(View view, String name,TakeScreenShotCallBack callback) throws Exception{

      takeScreenShot(view,name,SCREEN_SHOTS_LOCATION,callback);

      }

      TakeScreenShotCallBack mCallback = new TakeScreenShotCallBack(){

      @Override

      public void excute(File file) {

      // TODO Auto-generated method stub

      }

      };

      /**

    • 把传入的view保存为图片

    • @param parent 事件触发视图,可以是button等

    • @param view 保存为图片的视图

    • @param isShow 是否显示展示视图

    • @return
      */

      public void shotViewAsImage(View parent,View view, boolean isShow){

      PopupWindow popupWindow = getPopuptWindow(view);

      if(isShow)

      popupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);

      File newFile = null;

      try {

      takeScreenShot(popupWindow.getContentView(),null,mCallback);

      } catch (Exception e) {

      Log.e(TAG, e.getMessage());

      }

      }

      /**

    • 创建popupWindow

    • @param popupWindow_view

    • @return
      */

      protected PopupWindow getPopuptWindow(View popupWindow_view) {

      PopupWindow popupWindow = null;

      popupWindow = new PopupWindow(popupWindow_view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);// 创建PopupWindow实例

      popupWindow.setBackgroundDrawable(new BitmapDrawable());

      popupWindow.setFocusable(true);

      popupWindow.setOutsideTouchable(false);

      return popupWindow;

      }

      public interface TakeScreenShotCallBack{

      public void excute(File file);

      }

      /**

    • 对当前view进行截屏,并保存到path指定的目录下

    • @param view

    • @param name

    • @param path

    • @return

    • @throws Exception
      */

      public void takeScreenShot(final View view, final String name,final String path,final TakeScreenShotCallBack callback) throws Exception {

      final Bitmap bitmap = convertViewToBitmap(view);

      Runnable runnable = new Runnable() {

      @Override

      public void run() {

      Canvas canvas = new Canvas(bitmap);

      int w = bitmap.getWidth();

      int h = bitmap.getHeight();

      Paint paint = new Paint();

      paint.setColor(Color.YELLOW);

      String n = null;

      if(null == name || "".equals(name)){

      SimpleDateFormat simple = new SimpleDateFormat("yyyyMMddhhmmss");

      n = simple.format(new Date());

      }else{

      n = name;

      }

      canvas.drawText("", w - w / 2, h - h / 10, paint);

      canvas.save();

      canvas.restore();

      FileOutputStream fos = null;

      File file = null;

      try {

      File sddir = new File(path);

      if (!sddir.exists()) {

      sddir.mkdirs();

      }

      //生成以时间为名称的文件

      file = new File(path + File.separator

      • n + ".png");
        fos = new FileOutputStream(file);
        if (fos != null) {
        bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
        fos.close();
        }
        } catch (Exception e) {
        Log.e(TAG, e.getMessage(),e);
        }
        callback.excute(file);
        }
        };
        Thread thread = new Thread(runnable);
        thread.start();

      }

      /**

    • 转换view为bitmap

    • @param view

    • @return
      */

      public Bitmap convertViewToBitmap(View view){

      view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

      view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

      view.buildDrawingCache();

      Bitmap bitmap = view.getDrawingCache();

      return bitmap;

      }

      private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

      }

    调用函数

    public void print(String data) {

    View view = LayoutInflater.from(this).inflate(R.layout.print_test,

    null, false);

    //这几句可以注释掉,是另一个条形码生成的方法

    /* ImageView bar_img = (ImageView) view
    .findViewById(R.id.print_img_barcode);
    try {
    bar_img.setImageBitmap(testCODE39(data));
    } catch (Exception e) {
    } */

    PrintUtil printer = new PrintUtil();

    printer.shotViewAsImage(btn_print, view, true);

    }

    评论

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号