如何在iText Pdf中设置SD卡图像

时间:2018-03-21 11:14:16

标签: android itext

我想在pdf.i中设置图像使用iText设置图像在pdf.I Succsessfully Diaplayed image来自Assets文件夹。但我想从SD卡路径显示图像。 以下是资产文件夹中显示图像的代码。

    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();
    }

1 个答案:

答案 0 :(得分:1)

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();
        }