下面是我处理logo打印的代码。 Logo 位于 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;
}
最佳答案
是的,你应该在 assets 或 raw 目录下添加这种类型的资源...
但是如果你有任何限制
并且你只需要字节数组就可以试试
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();
关于android - 如何获取可绘制对象的文件实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11185348/