iText,将图像添加到 pdf 时出现 FileNotFoundException

iText, FileNotFoundException when adding image to pdf

我正在尝试使用 iText 将图像添加到我的 pdf 中,当按照 iTexts 文档进行操作时,它只是简单地告诉我这样做:

Image img = Image.getInstance("res/drawable/toplogos.png");
document.add(img);

但是我遇到了 FileNotFoundException。这是为什么?该文件位于可绘制文件夹中。

谢谢。

试试这个,希望对你有帮助

 Drawable d = getResources().getDrawable(R.drawable.toplogos)
 BitmapDrawable bitDw = ((BitmapDrawable) d);
 Bitmap bmp = bitDw.getBitmap();  
 ByteArrayOutputStream stream = new ByteArrayOutputStream();
 bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
 Image image = Image.getInstance(stream.toByteArray());
 document.add(image);