sns*_*sns 10 pdf android itext drawable
我想使用iText将图像添加到android PDF.我想首先实现这一目标,而不是将图像保存到SDCard.我将我的图像放入res/drawable文件夹,但证明图像路径不起作用,它会抛出FileNotFound Exception.我的道路是这样的:
String path = “res/drawable/myImage.png”
Image image = Image.getInstance(path);
document.add(image);
Run Code Online (Sandbox Code Playgroud)
现在请建议我如何为getInstance(...)方法添加正确的文件路径.谢谢
Fes*_*loe 31
当然它不会那样工作.
将您的图像移动到assets文件夹以使用getassets()方法访问它
// load image
try {
// get input stream
InputStream ims = getAssets().open("myImage.png");
Bitmap bmp = BitmapFactory.decodeStream(ims);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image image = Image.getInstance(stream.toByteArray());
document.add(image);
}
catch(IOException ex)
{
return;
}
Run Code Online (Sandbox Code Playgroud)
小智 11
我为您的问题找到了解决方案.如果您想从可绘制文件夹中获取图像并使用iText将其放入PDF文件,请使用以下代码:
try {
document.open();
Drawable d = getResources().getDrawable(R.drawable.myImage);
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);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22393 次 |
| 最近记录: |