2013-07-30 48 views
1

我試着採用了android文本做出PDF和日誌給了我這個消息時,我嘗試生成它:在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) 

我的繼承人清單文件:

<?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> 

生病欣賞幫助。

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

}  

}

回答

0

這可能聽起來很奇怪,但你檢查你的手機有一個SD卡。只是一個通知,因爲我犯了以前的錯誤。另外,當您創建文件時,請檢查您是否使用了正確的模式,以防您的PDF由其他活動創建。

0

我認爲問題是,你必須投入文本之前創建的文件。但是你應該發佈一些代碼。

編輯1:

Error is here?

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

如果沒有發佈錯誤所在的行。

如果是上述情況,我認爲你沒有任何外部存儲(如SD卡)。你做?在這種情況下,試試這個:

可以使用

context.getCacheDir(); 

您可以創建文件做得到內部存儲路徑:

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

我轉發了我的代碼。 – Miguel