0

Hello guys i have the problem that i want to include an image in my project and when the user click the print button the image will be printet followed by other information but i dont get it to pass the image path to the PrintTool

PrintTool.printPhotoWithPath(imagePath, this);

and the first line in printtool are this

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");
    }
}

So my problem is, how can i get the path from my image in drawable folder to the code?

Selva
  • 338
  • 2
  • 12

2 Answers2

0

Please remove this line

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

Provided the image is currently in your drawable directory, you can get it like this:

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");
}

NB Replace with the name of your Drawable.

If you require the path of the Bitmap, by default it is

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

Refer to this.

Community
  • 1
  • 1
TejjD
  • 2,571
  • 1
  • 17
  • 37
  • so i have tried your method but now it says Cannot Resolve method getResources() – Christian Lerch Dec 30 '15 at 16:37
  • Sorry you need context. Try 'getContext().getResources()', depending on whether it's a Fragment or Activity, you can also try 'getApplicationContext().getResources()' – TejjD Dec 30 '15 at 17:10
  • Its nothing of the above mentioned this is in an Activity PrintTool.printPhotoWithPath(imagePath, this); the other stuff is just PrintTool.java its just a class – Christian Lerch Dec 30 '15 at 20:05
0

i hope this helps you

File file  = new File(String.valueOf(R.mipmap.imgeName));
        file.getPath();
Ben_Gratvol
  • 84
  • 1
  • 12