【问题标题】:Print logo on top of recipe android在配方 android 上打印徽标
【发布时间】:2016-04-04 10:28:19
【问题描述】:

大家好,我有一个问题,我想在我的项目中包含一个图像,当用户单击打印按钮时,图像将被打印,然后是其他信息,但我没有将图像路径传递给 PrintTool

PrintTool.printPhotoWithPath(imagePath, this);

printtool 的第一行就是这个

public static void printPhotoWithPath(String filePath, Context context) {

    // Get the picture based on the path
    File mfile = new File(filePath/*path*/);
    if (mfile.exists()) {
        Bitmap bmp = BitmapFactory.decodeFile(filePath/*path*/);
        byte[] command = decodeBitmap(bmp);
        printPhoto(command, context);


    }else{
        Log.e("PrintTools_58mm", "the file isn't exists");
    }
}

所以我的问题是,我怎样才能从可绘制文件夹中的图像到代码的路径?

【问题讨论】:

    标签: android string path android-drawable


    【解决方案1】:

    请删除此行

    File mfile = new File(filePath/*path*/);
    

    如果图片当前在你的drawable目录中,你可以这样获取:

    if (mfile.exists()) {
        Bitmap bmp = BitmapFactory.decodeFile(getResources(), R.drawable.<yourDrawableName>);
        byte[] command = decodeBitmap(bmp);
        printPhoto(command, context);
    }else{
        Log.e("PrintTools_58mm", "the file isn't exists");
    }
    

    注意替换为您的 Drawable 的名称。

    如果需要Bitmap的路径,默认是

    String imageUri = "drawable://" + R.drawable.image;
    

    Refer to this.

    【讨论】:

    • 所以我试过你的方法,但现在它说Cannot Resolve method getResources()
    • 抱歉,您需要上下文。试试'getContext().getResources()',看是Fragment还是Activity,也可以试试'getApplicationContext().getResources()'
    • 上面没有提到 this 在 Activity PrintTool.printPhotoWithPath(imagePath, this);其他的只是 PrintTool.java 它只是一个类
    【解决方案2】:

    希望对你有帮助

    File file  = new File(String.valueOf(R.mipmap.imgeName));
            file.getPath();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-25
      • 1970-01-01
      • 2014-03-07
      • 1970-01-01
      • 2015-05-28
      • 2020-01-26
      • 2021-02-16
      • 1970-01-01
      相关资源
      最近更新 更多