如何获取drawable的File实例?

use*_*534 9 android uri file fileinputstream drawable

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

java.io.FileNotFoundException: /android.resource:/com.android.test/2130837505 (No such file or directory).
Run Code Online (Sandbox Code Playgroud)

有什么建议?

    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;
    }
Run Code Online (Sandbox Code Playgroud)

Dhe*_*ngh 10

是的你应该在资产或原始目录下添加这种类型的资源...

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

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();
Run Code Online (Sandbox Code Playgroud)