9

Below is the code that I cope with logo printing. The logo is placed in res/drawable folder. When I run the app, it throws:

java.io.FileNotFoundException: /android.resource:/com.android.test/2130837505 (No such file or directory).

Any advice?

    public  boolean printLogo()
    {
      Uri logo_path = Uri.parse("android.resource://com.android.test/" + R.drawable._logo);
      File logo = new File(logo_path.toString());
      byte[] logo_bytes = new byte[(int) logo.length()];
      System.out.print("Length:" + logo.length());
      FileInputStream fs;
      try {
          fs = new FileInputStream(logo);
          fs.read(logo_bytes);
          fs.close();
          mChatService.write(logo_bytes);
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      }catch (IOException e) {
          e.printStackTrace();
      }
      return true;
    }
Mauker
  • 11,237
  • 7
  • 58
  • 76
user1437534
  • 129
  • 3
  • 8
  • can u explain your problem what u are trying to do? – ρяσѕρєя K Jun 25 '12 at 08:00
  • I try to print a bitmap via bluetooth-connected printer. `mChatService.write(Byte[] b[])` is where `OutputStream.write(Byte[] b[])` is executed. In that case, I need transfer a bitmap to a byte array. I placed the bitmap in res/drawable folder, and wrote the code above, then I encountered the FileNotFoundException just as I described. – user1437534 Jun 25 '12 at 08:20
  • see Dheeresh answer i think this will help u – ρяσѕρєя K Jun 25 '12 at 08:23

2 Answers2

10

yes you should add the resource of such type under assets or raw directory...

but if you have any limitation ans you only need byte array can try

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

  ByteArrayOutputStream stream = new ByteArrayOutputStream();
  bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
   byte[] byteArray = stream.toByteArray();
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
1

Put your image resources under assets folder first, then can use the AssetManager for getting InputStream from resource.

AssetManager mgr = context.getAssets(); 
FileInputStream fin = (FileInputStream)mgr.open("path/filename");

path should not contain the assets folder.

Ron
  • 24,175
  • 8
  • 56
  • 97