gpt4 book ai didi

android - 如何在 iText Pdf 中设置来自 SD 卡的图像

转载 作者:行者123 更新时间:2023-11-30 00:03:50 24 4
gpt4 key购买 nike

我想在 pdf 中设置图像。我使用 iText 在 pdf 中设置图像。我成功地显示了 Assets 文件夹中的图像。但我想显示来自 sd 卡路径的图像。以下是显示 Assets 文件夹中图像的代码。

    try
{
InputStream ims = con.getAssets().open("prof_image.png");
Bitmap bmp = BitmapFactory.decodeStream(ims);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image image = Image.getInstance(stream.toByteArray());
image.setAlignment(Image.ALIGN_RIGHT);

cell.addElement(image);
//cell.setHorizontalAlignment(Element.ALIGN_CENTER); //alignment
//cell.setBackgroundColor(new GrayColor(0.75f)); //cell background color
cell.setFixedHeight(60); //cell height
table.addCell(cell);
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}

最佳答案

try
{
String photoPath = Environment.getExternalStorageDirectory()+"/gtu.png";
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bmp = BitmapFactory.decodeFile(photoPath, options);

//InputStream ims = con.getAssets().open("prof_image.png");
//Bitmap bmp = BitmapFactory.decodeStream(ims);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image image = Image.getInstance(stream.toByteArray());
image.setAlignment(Image.ALIGN_RIGHT);

cell.addElement(image);
//cell.setHorizontalAlignment(Element.ALIGN_CENTER); //alignment
//cell.setBackgroundColor(new GrayColor(0.75f)); //cell background color
cell.setFixedHeight(60); //cell height
table.addCell(cell);
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}

关于android - 如何在 iText Pdf 中设置来自 SD 卡的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49404840/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com