How to get a File instance of a drawable?

user1437534 picture user1437534 · Jun 25, 2012 · Viewed 9.6k times · Source

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

Answer

Dheeresh Singh picture Dheeresh Singh · Jun 25, 2012

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();