9

下面是我处理徽标打印的代码。徽标放置在 res/drawable 文件夹中。当我运行该应用程序时,它会抛出:

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

有什么建议吗?

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

2 回答 2

10

是的,您应该在资产或原始目录下添加此类资源...

但是如果你have any limitation只需要字节数组可以试试

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();
于 2012-06-25T08:07:36.317 回答
1

先把你的图片资源放到assets文件夹下,然后就可以用AssetManagerfor get InputStreamfrom resource了。

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

path不应包含资产文件夹。

于 2012-06-25T08:00:05.123 回答