【问题标题】:Can't open PDF in PDF viewer apps [duplicate]无法在 PDF 查看器应用程序中打开 PDF [重复]
【发布时间】:2014-10-16 00:02:18
【问题描述】:

我有一个类可以打开存储在我的原始资源中的空白字符表(用于 LARP),然后使用 iText 向空字段添加一些值。 PDF 存储在 /data/data/package/files 中就好了,但是当我尝试使用 OpenPDF 方法时,我收到 EACCES (permission denied) 错误。如果我尝试使用 adb 将其导出到我的计算机,我可以在 PDF 查看器/编辑器中毫无问题地打开它。

对于 android 开发来说还是相当新的,所以我不知道为什么我会收到 Permission Denied。

imports...

/**
 * Created by Matt on 10/15/2014.
 */
public class SheetBuilder {

    private final String LOGTAG = getClass().getName();

    private Context context;
    private InputStream inputStream;
    private OutputStream outputStream = null;
    private PdfReader reader = null;
    private Document document;
    private PdfWriter writer = null;
    private String outputFileName;
    private PdfContentByte canvas;

    private int alignment = Element.ALIGN_LEFT;

    public SheetBuilder(Context context, int sourceFile, String outputFileName) {
        this.outputFileName = outputFileName.replace(".pdf", "") + ".pdf";
        this.context = context;

        inputStream = context.getResources().openRawResource(sourceFile);
        try {
            reader = new PdfReader(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            outputStream = context.openFileOutput(this.outputFileName, context.MODE_PRIVATE);
        } catch (Exception e) {
            e.printStackTrace();
        }

        document = new Document(reader.getPageSize(1));
        try {
            writer = PdfWriter.getInstance(document, outputStream);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        document.open();
        canvas = writer.getDirectContent();
    }

    public void OpenPDF() {
        ContextWrapper cw = new ContextWrapper(context);
        File path = cw.getFilesDir();
        File pdfFile = new File(path + "/" + outputFileName);
        if(pdfFile.exists())
        {
            Log.i(LOGTAG, "Found " + outputFileName);

            Uri uriPath = Uri.fromFile(pdfFile);
            Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
            pdfIntent.setDataAndType(uriPath, "application/pdf");
            pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            try {
                context.startActivity(pdfIntent);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(context, "No application available to view PDF", Toast.LENGTH_LONG).show();
            }
        }
    }

    public void ImportPage(int PageNumber) {
        if(PageNumber == 0) PageNumber = 1;
        PdfImportedPage page = writer.getImportedPage(reader, PageNumber);
        document.newPage();
        canvas.addTemplate(page, 0, 0);
    }

    public void setAlignment(int Alignment) {
        this.alignment = Alignment;
    }

    public void setAlignment() {
        this.alignment = Element.ALIGN_LEFT;
    }

    public void AddBasicPhrase(String phrase, float x, float y, float rotation) {
        Phrase p = new Phrase(phrase);
        ColumnText.showTextAligned(canvas, alignment, p, x, y, rotation);
    }

    public void AddBasicPhrase(String phrase, float x, float y) {
        AddBasicPhrase(phrase, x, y, 0);
    }

    public void Close() {
        document.close();

        if (outputStream != null) {
            try {
                outputStream.flush();
                outputStream.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

【问题讨论】:

  • 我确信它之前已经得到了回答,但是当我搜索它时,我不断得到一些没有回答我的问题的东西,而且我没有看到上面发布的那个。那些似乎会回答我的问题的人一直给我其他错误,直到我弄清楚如何让它发挥作用。

标签: java android pdf


【解决方案1】:

找到了我自己的解决方案。下面用 cmets 标记的更改。如果我在做我不应该做的事情,请纠正我。发现“外部与内部”文件存储是我的问题。

/**
 * Created by Matt on 10/15/2014.
 */
public class SheetBuilder {

    private final String LOGTAG = getClass().getName();

    private Context context;
    private InputStream inputStream;
    private OutputStream outputStream = null;
    private PdfReader reader = null;
    private Document document;
    private PdfWriter writer = null;
    private String outputFileName;
    private PdfContentByte canvas;

    private int alignment = Element.ALIGN_LEFT;

    public SheetBuilder(Context context, int sourceFile, String outputFileName) {
        this.outputFileName = outputFileName.replace(".pdf", "") + ".pdf";
        this.context = context;

        inputStream = context.getResources().openRawResource(sourceFile);
        try {
            reader = new PdfReader(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

        /* Changes From Here */    
        try {
            File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
            File file = new File(path, this.outputFileName);
            Log.i("ExternalStorage", file.getAbsolutePath());
            path.mkdirs();
            outputStream = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        /* To Here */

        document = new Document(reader.getPageSize(1));
        try {
            writer = PdfWriter.getInstance(document, outputStream);
        } catch (DocumentException e) {
            Log.e("ErrorsAllAround", "Nope!");
            e.printStackTrace();
        }
        document.open();
        canvas = writer.getDirectContent();
    }

    public void OpenPDF() {
        /* Changes From Here */
        File pdfFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), outputFileName);
        /* To Here */
        if(pdfFile.exists())
        {
            Log.i(LOGTAG, "Found " + pdfFile.getAbsolutePath());

            Uri uriPath = Uri.fromFile(pdfFile);
            Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
            pdfIntent.setDataAndType(uriPath, "application/pdf");
            pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            try {
                context.startActivity(pdfIntent);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(context, "No application available to view PDF", Toast.LENGTH_LONG).show();
            }
        }
        else
        {
            Log.i(LOGTAG, "File Not Found: " + pdfFile.getAbsolutePath());
            Toast.makeText(context, "File Not Found: " + pdfFile.getAbsolutePath(), Toast.LENGTH_LONG).show();
        }
    }

    public void ImportPage(int PageNumber) {
        if(PageNumber == 0) PageNumber = 1;
        PdfImportedPage page = writer.getImportedPage(reader, PageNumber);
        document.newPage();
        canvas.addTemplate(page, 0, 0);
    }

    public void setAlignment(int Alignment) {
        this.alignment = Alignment;
    }

    public void setAlignment() {
        this.alignment = Element.ALIGN_LEFT;
    }

    public void AddBasicPhrase(String phrase, float x, float y, float rotation) {
        Phrase p = new Phrase(phrase);
        ColumnText.showTextAligned(canvas, alignment, p, x, y, rotation);
    }

    public void AddBasicPhrase(String phrase, float x, float y) {
        AddBasicPhrase(phrase, x, y, 0);
    }

    public void Close() {
        document.close();

        if (outputStream != null) {
            try {
                outputStream.flush();
                outputStream.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-29
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    • 1970-01-01
    • 2019-01-26
    相关资源
    最近更新 更多