在android中的iText pdf库中访问资产文件夹

时间:2011-08-11 10:31:48

标签: android

我想使用iText库将一些图像从assets文件夹写入pdf文件。 如果我使用SD卡访问,那么它工作正常。 但是,如果我使用资产文件夹访问,那么它显示文件未找到异常。 我的代码是

package com.app.ipro;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;

import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;

import com.itextpdf.text.Anchor;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class MakeInvoice {
    private static String FILE = "";

    public static void main() {
        try {
            String externalStoragePath = Environment
                    .getExternalStorageDirectory().getAbsolutePath();
            System.out.println("externalStoragePath:" + externalStoragePath);
            boolean f = new File(externalStoragePath + "\\MyPDF").mkdirs();
            System.out.println("f:" + f);
            FILE = externalStoragePath + "\\MyPDF\\" + "FirstPdfabc6.pdf";
            Document document = new Document(PageSize.A4);
            PdfWriter.getInstance(document, new FileOutputStream(FILE));
            document.open();
            addMetaData(document);
            addTitlePage(document);
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private static void addMetaData(Document document) {
        document.addTitle("My first PDF");
        document.addSubject("Using iText");
        document.addKeywords("Java, PDF, iText");
        document.addAuthor("Lars Vogel");
        document.addCreator("Lars Vogel");
    }

    private static void addTitlePage(Document document)
            throws DocumentException {
        Paragraph preface = new Paragraph();
        addEmptyLine(preface, 1);
        preface.add(new Paragraph("Title of the document"));

        addEmptyLine(preface, 1);

        document.add(preface);
        String strLogo = "file:///android_asset/tmpl1/Vector_background1.jpg";


        PdfPTable table = new PdfPTable(2);
        table.setTableEvent(new tableEvent(strLogo));
        table.addCell("Name");
        table.addCell("Place");
        table.addCell("RoseIndia");
        table.addCell("Delhi");
        document.add(table);

        document.newPage();
    }

    private static void addEmptyLine(Paragraph paragraph, int number) {
        for (int i = 0; i < number; i++) {
            paragraph.add(new Paragraph(" "));
        }
    }
}

并且在表格中我写了

package com.app.ipro;

import java.io.IOException;
import java.net.MalformedURLException;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPTableEvent;


public class tableEvent implements PdfPTableEvent{
    String img;
    public tableEvent(String imgPath){
        img = imgPath;
    }

    @Override
    public void tableLayout(PdfPTable arg0, float[][] arg1, float[] arg2,
            int arg3, int arg4, PdfContentByte[] arg5) {
        // TODO Auto-generated method stub
        PdfContentByte pdfContentByte = arg5[PdfPTable.BACKGROUNDCANVAS];
        Image bgImage;
        try {
            bgImage = Image.getInstance(img);
            bgImage.setAbsolutePosition(10, 10);
            bgImage.setWidthPercentage(50);
            pdfContentByte.addImage(bgImage);
        } catch (BadElementException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

所以运行时它显示异常,

08-11 15:24:45.817: WARN/System.err(18022): java.io.FileNotFoundException: /android_asset/tmpl1/Vector_background1.jpg (No such file or directory)
08-11 15:24:45.837: WARN/System.err(18022):     at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
08-11 15:24:45.837: WARN/System.err(18022):     at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
08-11 15:24:45.847: WARN/System.err(18022):     at java.io.FileInputStream.<init>(FileInputStream.java:82)
08-11 15:24:45.857: WARN/System.err(18022):     at org.apache.harmony.luni.internal.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:85)
08-11 15:24:45.857: WARN/System.err(18022):     at org.apache.harmony.luni.internal.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:184)
08-11 15:24:45.867: WARN/System.err(18022):     at java.net.URL.openStream(URL.java:653)
08-11 15:24:45.867: WARN/System.err(18022):     at com.itextpdf.text.Image.getInstance(Image.java:245)
08-11 15:24:45.877: WARN/System.err(18022):     at com.itextpdf.text.Image.getInstance(Image.java:341)
08-11 15:24:45.877: WARN/System.err(18022):     at com.app.ipro.tableEvent.tableLayout(tableEvent.java:28)
08-11 15:24:45.897: WARN/System.err(18022):     at com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:731)
08-11 15:24:45.897: WARN/System.err(18022):     at com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:824)
08-11 15:24:45.907: WARN/System.err(18022):     at com.itextpdf.text.pdf.ColumnText.goComposite(ColumnText.java:1524)
08-11 15:24:45.907: WARN/System.err(18022):     at com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:807)
08-11 15:24:45.907: WARN/System.err(18022):     at com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:795)
08-11 15:24:45.907: WARN/System.err(18022):     at com.itextpdf.text.pdf.PdfDocument.addPTable(PdfDocument.java:2361)
08-11 15:24:45.907: WARN/System.err(18022):     at com.itextpdf.text.pdf.PdfDocument.add(PdfDocument.java:673)
08-11 15:24:45.917: WARN/System.err(18022):     at com.itextpdf.text.Document.add(Document.java:282)
08-11 15:24:45.927: WARN/System.err(18022):     at com.app.ipro.MakeInvoice.addTitlePage(MakeInvoice.java:87)
08-11 15:24:45.927: WARN/System.err(18022):     at com.app.ipro.MakeInvoice.main(MakeInvoice.java:45)
08-11 15:24:45.937: WARN/System.err(18022):     at com.app.ipro.Other$1.onClick(Other.java:47)
08-11 15:24:45.937: WARN/System.err(18022):     at android.view.View.performClick(View.java:2408)
08-11 15:24:45.937: WARN/System.err(18022):     at android.view.View$PerformClick.run(View.java:8816)
08-11 15:24:45.947: WARN/System.err(18022):     at android.os.Handler.handleCallback(Handler.java:587)
08-11 15:24:45.947: WARN/System.err(18022):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-11 15:24:45.967: WARN/System.err(18022):     at android.os.Looper.loop(Looper.java:123)
08-11 15:24:45.967: WARN/System.err(18022):     at android.app.ActivityThread.main(ActivityThread.java:4627)
08-11 15:24:45.977: WARN/System.err(18022):     at java.lang.reflect.Method.invokeNative(Native Method)
08-11 15:24:45.977: WARN/System.err(18022):     at java.lang.reflect.Method.invoke(Method.java:521)
08-11 15:24:45.987: WARN/System.err(18022):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-11 15:24:45.987: WARN/System.err(18022):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-11 15:24:45.987: WARN/System.err(18022):     at dalvik.system.NativeStart.main(Native Method)

如果我从SD卡引用相同的图像,那么它的工作完美,但我想使用资源文件夹。 请帮我出来

1 个答案:

答案 0 :(得分:0)

我们只能通过输入流从资源打开图像。首先打开图像作为输入流,然后将其保存到临时位置,然后将保存的图像路径提供给tableEvent。