使用带有android的IText创建PDF时获取IOException

时间:2014-09-08 09:33:50

标签: java android pdf cordova

我正在尝试使用Itext创建PDF时遇到 IOException 。 错误说我试图写入只读存储,因为我在清单文件中添加了所需的权限。

我正在使用 Cordova 3.5

以下是代码:

public class PdfCreator {
String TAG = "PdfCreator";

public void createPdf(){
    try {
        Log.d(TAG, Environment.getExternalStorageDirectory().getPath() + "/Hello.pdf");            
        OutputStream file = new FileOutputStream(new File(Environment.getExternalStorageDirectory().getPath() + "/Hello.pdf"));            
        Document document = new Document();
        PdfWriter.getInstance(document, file); 
        document.open();
        document.add(new Paragraph("Hello world, iText"));
        document.close();
        file.close();

    } catch (Exception e) {

        e.printStackTrace();
    }
}

Stake Trace:

09-08 05:15:42.501: W/System.err(1114): java.io.FileNotFoundException: /storage/sdcard/Hello.pdf:          open failed: EROFS (Read-only file system)
09-08 05:15:42.531: W/System.err(1114):     at libcore.io.IoBridge.open(IoBridge.java:409)
09-08 05:15:42.531: W/System.err(1114):     at java.io.FileOutputStream.<init>     (FileOutputStream.java:88)
09-08 05:15:42.581: W/System.err(1114):     at java.io.FileOutputStream.<init>   (FileOutputStream.java:73)
09-08 05:15:42.581: W/System.err(1114):     at com.idsil.pdfapp.PdfCreator.createPdf(PdfCreator.java:19)
09-08 05:15:42.581: W/System.err(1114):     at com.idsil.pdfapp.PdfApp.onCreate(PdfApp.java:36)
09-08 05:15:42.581: W/System.err(1114):     at android.app.Activity.performCreate(Activity.java:5133)
09-08 05:15:42.581: W/System.err(1114):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-08 05:15:42.581: W/System.err(1114):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-08 05:15:42.581: W/System.err(1114):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-08 05:15:42.591: W/System.err(1114):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-08 05:15:42.591: W/System.err(1114):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-08 05:15:42.631: W/System.err(1114):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-08 05:15:42.631: W/System.err(1114):     at android.os.Looper.loop(Looper.java:137)
09-08 05:15:42.658: W/System.err(1114):     at android.app.ActivityThread.main(ActivityThread.java:5103)
09-08 05:15:42.661: W/System.err(1114):     at java.lang.reflect.Method.invokeNative(Native Method)
09-08 05:15:42.661: W/System.err(1114):     at java.lang.reflect.Method.invoke(Method.java:525)
09-08 05:15:42.661: W/System.err(1114):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-08 05:15:42.661: W/System.err(1114):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-08 05:15:42.661: W/System.err(1114):     at dalvik.system.NativeStart.main(Native Method)
09-08 05:15:42.691: W/System.err(1114): Caused by: libcore.io.ErrnoException: open failed: EROFS (Read-only file system)
09-08 05:15:42.691: W/System.err(1114):     at libcore.io.Posix.open(Native Method)
09-08 05:15:42.691: W/System.err(1114):     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
09-08 05:15:42.691: W/System.err(1114):     at libcore.io.IoBridge.open(IoBridge.java:393)

感谢任何帮助。

提前致谢。

2 个答案:

答案 0 :(得分:1)

试试这个,

Environment.getExternalStorageDirectory().getAbsolutePath()+"/Hello.pdf"

use .getAbsolutePath() instead of .getPath()..

以这种方式创建文件,

InputStream input = null;
OutputStream output = null;

File folder = new File("/sdcard", "hellofolder");
folder.mkdirs();

File myFile = new File("/sdcard/hellofolder/hello.pdf");
output = new FileOutputStream(myFile);

Mainfest.xml

将此添加为清单的子项

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

或尝试使用真实设备或创建新模拟器并将SD卡大小设置为200及以上,

答案 1 :(得分:0)

试试这个     eString filePath = context.getFilesDir().getPath().toString() + "/Hello.pdf";     File f = new File(filePath);     OutputStream file = new FileOutputStream(f);