简体   繁体   English

在Android中创建PDF时出错?

[英]Error creating PDF in android?

Im trying to make a PDF using android text and the log gives me this message when I try to generate it: 我试图使用android文本制作PDF,当我尝试生成它时,日志显示了此消息:

07-29 19:45:23.682: D/PDFCreator(12569): PDF Path: /mnt/sdcard/droidText
07-29 19:45:23.682: E/PDFCreator(12569): ioException:java.io.FileNotFoundException: /mnt/sdcard/droidText/sample.pdf (Permission denied)
07-29 19:45:27.456: D/PDFCreator(12569): PDF Path: /mnt/sdcard/droidText
07-29 19:45:27.456: E/PDFCreator(12569): ioException:java.io.FileNotFoundException: /mnt/sdcard/droidText/sample.pdf (Permission denied)

Heres my manifest file: 这是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ex.pruebapdf"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-sdk
    android:minSdkVersion="9"
    android:targetSdkVersion="10" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.ex.pruebapdf.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Ill appreciate the help. 我不胜感激帮助。

Heres the mainactivity.java, its a preformatted code I found on the internet. 这是mainactivity.java,它是我在互联网上找到的预格式化代码。 :

package com.ex.pruebapdf;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfWriter;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

private Button b;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    b= (Button)findViewById(R.id.botoncin);
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            createPDF();

        }
    });
}

public void createPDF()
{
    Document doc = new Document();


     try {
            String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/droidText";

            File dir = new File(path);
                if(!dir.exists())
                    dir.mkdirs();

            Log.d("PDFCreator", "PDF Path: " + path);


            File file = new File(dir, "sample.pdf");
            FileOutputStream fOut = new FileOutputStream(file);

            PdfWriter.getInstance(doc, fOut);

            //open the document
            doc.open();


            Paragraph p1 = new Paragraph("Hi! I am generating my first PDF using DroidText");
            Font paraFont= new Font(Font.COURIER);
            p1.setAlignment(Paragraph.ALIGN_CENTER);
            p1.setFont(paraFont);

             //add paragraph to document    
             doc.add(p1);

             Paragraph p2 = new Paragraph("This is an example of a simple paragraph");
             Font paraFont2= new Font(Font.COURIER,14.0f,Color.GREEN);
             p2.setAlignment(Paragraph.ALIGN_CENTER);
             p2.setFont(paraFont2);

             doc.add(p2);

             ByteArrayOutputStream stream = new ByteArrayOutputStream();
             Image myImg = Image.getInstance(stream.toByteArray());
             myImg.setAlignment(Image.MIDDLE);

             //add image to document
             doc.add(myImg);

             //set footer
             Phrase footerText = new Phrase("This is an example of a footer");
             HeaderFooter pdfFooter = new HeaderFooter(footerText, false);
             doc.setFooter(pdfFooter);



     } catch (DocumentException de) {
             Log.e("PDFCreator", "DocumentException:" + de);
     } catch (IOException e) {
             Log.e("PDFCreator", "ioException:" + e);
     } 
     finally
     {
             doc.close();
     }

}      

} }

I think the problem is that you have to create the file before putting into text. 我认为问题在于您必须先创建文件,然后再输入文本。 But you should post some code. 但是您应该发布一些代码。

EDIT 1: 编辑1:

Error is here? 错误在这里?

String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/droidText";

If not post the row where error is. 如果没有,则将错误发布到该行。

In case it is the above, I think you don't have any external storage (like an SD card). 如果是上述情况,我认为您没有任何外部存储设备(例如SD卡)。 Do you? 你呢? In that case try this: 在这种情况下,请尝试以下操作:

You can get internal storage path using 您可以使用以下命令获取内部存储路径

context.getCacheDir();

You can create file doing: 您可以创建文件来执行以下操作:

File temp = new File(context.getCacheDir(),"you_file_name");

It may sound weird but have you checked your phone has a SD card. 听起来可能很奇怪,但是您是否已检查手机是否装有SD卡。 Just a notice because I made that mistake before. 只是通知,因为我之前犯过这个错误。 In addition, when you create the file, check if you use the correct mode, in case your PDF is created by other activities. 此外,在创建文件时,请检查是否使用正确的模式,以防PDF是由其他活动创建的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM