繁体   English   中英

我在获取正确的文件 URI 以将徽标打印到迷你打印机时遇到问题

[英]I have a problems getting the correct file URI in order to print logo to a mini printer

我正在尝试将徽标(图像)从我的应用程序打印到我的蓝牙打印机。 我在网上特别是通过这个网站搜索了一个解决方案,但我仍然没有解决我的最终问题,即将图像打印到迷你打印机。 有许多提供的答案,其中许多我不得不适应解决我自己的问题,但我仍然无法完成它。 我使用了示例代码 print_image(String File)- 如下所示,它接收文件路径(应该引用可绘制图像资源)作为字符串,但不幸的是,我从我的代码中收到“文件不存在”错误,因为显然,应用程序无法正确引用路径(我最头疼的问题)。

因此,我需要有关如何将路径正确定位为字符串并传递给 print_image(String File) 模块的帮助。 我在这个上碰到了一堵砖墙。 谢谢你。

private void print_image(String file) {
     File fl = new File(file);
     if (fl.exists()) {
         Bitmap bmp = BitmapFactory.decodeFile(file);
        convertBitmap(bmp);
        mService.write(PrinterCommands.SET_LINE_SPACING_24);

        int offset = 0;
        while (offset < bmp.getHeight()) {
            mService.write(PrinterCommands.SELECT_BIT_IMAGE_MODE);
            for (int x = 0; x < bmp.getWidth(); ++x) {

                for (int k = 0; k < 3; ++k) {

                    byte slice = 0;
                    for (int b = 0; b < 8; ++b) {
                        int y = (((offset / 8) + k) * 8) + b;
                        int i = (y * bmp.getWidth()) + x;
                        boolean v = false;
                        if (i < dots.length()) {
                            v = dots.get(i);
                        }
                        slice |= (byte) ((v ? 1 : 0) << (7 - b));
                    }
                    mService.write(slice);
                }
            }
            offset += 24;
            mService.write(PrinterCommands.FEED_LINE);
            mService.write(PrinterCommands.FEED_LINE);          
            mService.write(PrinterCommands.FEED_LINE);
            mService.write(PrinterCommands.FEED_LINE);
            mService.write(PrinterCommands.FEED_LINE);
            mService.write(PrinterCommands.FEED_LINE);
        }
        mService.write(PrinterCommands.SET_LINE_SPACING_30);


    } else {
        Toast.makeText(this, "file doesn't exists", Toast.LENGTH_SHORT)
                .show();
    }
}


public String getURLForResource (int resourceId) {
    return Uri.parse("android.resource://"+R.class.getPackage().getName()+"/" +resourceId).toString();
}

这就是我调用 print_image(File) 1. fileString = getURLForResource(R.drawable.imageName) 2. 将返回的 Uri(类似于 android.resource://com.myAppBase.com/4435664647)作为字符串 3. 调用 print_image(fileString)

因此,在测试 print_image(file) 中是否存在文件时,

我收到消息“文件不存在”。
请问在正确的道路上做错了什么?

尝试使用 Uri 执行此操作,然后将 Uri 转换为文件 InputStream,然后将 InputStream 转换为 Bitmap。

InputStream inputStream = getContentResolver().openInputStream(uri);

BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);

Bitmap bmp = BitmapFactory.decodeStream(bufferedInputStream);

然后打印 bitmap。

Bitmap bmp = BitmapFactory.decodeFile(file); 

您不能将文件 class 用于资源。 更好的:

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.imageName);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM