Java 使用IText和android创建PDF时获取IOException

Java 使用IText和android创建PDF时获取IOException,java,android,pdf,cordova,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() +

我尝试使用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();
    }
}
桩迹:

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)
09-08 05:15:42.501:W/System.err(1114):java.io.FileNotFoundException:/storage/sdcard/Hello.pdf:open失败:EROFS(只读文件系统)
09-08 05:15:42.531:W/System.err(1114):位于libcore.io.IoBridge.open(IoBridge.java:409)
09-08 05:15:42.531:W/System.err(1114):位于java.io.FileOutputStream。(FileOutputStream.java:88)
09-08 05:15:42.581:W/System.err(1114):位于java.io.FileOutputStream。(FileOutputStream.java:73)
09-08 05:15:42.581:W/System.err(1114):位于com.idsil.pdfapp.PdfCreator.createPdf(PdfCreator.java:19)
09-08 05:15:42.581:W/System.err(1114):位于com.idsil.pdfapp.pdfapp.onCreate(pdfapp.java:36)
09-08 05:15:42.581:W/System.err(1114):位于android.app.Activity.performCreate(Activity.java:5133)
09-08 05:15:42.581:W/System.err(1114):位于android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-08 05:15:42.581:W/System.err(1114):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-08 05:15:42.581:W/System.err(1114):位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-08 05:15:42.591:W/System.err(1114):在android.app.ActivityThread.access$600(ActivityThread.java:141)
09-08 05:15:42.591:W/System.err(1114):位于android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-08 05:15:42.631:W/System.err(1114):位于android.os.Handler.dispatchMessage(Handler.java:99)
09-08 05:15:42.631:W/System.err(1114):位于android.os.Looper.loop(Looper.java:137)
09-08 05:15:42.658:W/System.err(1114):位于android.app.ActivityThread.main(ActivityThread.java:5103)
09-08 05:15:42.661:W/System.err(1114):位于java.lang.reflect.Method.invokenactive(本机方法)
09-08 05:15:42.661:W/System.err(1114):位于java.lang.reflect.Method.invoke(Method.java:525)
09-08 05:15:42.661:W/System.err(1114):在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-08 05:15:42.661:W/System.err(1114):位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-08 05:15:42.661:W/System.err(1114):在dalvik.System.NativeStart.main(本机方法)
09-08 05:15:42.691:W/System.err(1114):原因:libcore.io.ErrnoException:打开失败:EROFS(只读文件系统)
09-08 05:15:42.691:W/System.err(1114):位于libcore.io.Posix.open(本机方法)
09-08 05:15:42.691:W/System.err(1114):位于libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
09-08 05:15:42.691:W/System.err(1114):位于libcore.io.IoBridge.open(IoBridge.java:393)
感谢您的帮助

提前谢谢。

试试这个

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或更大,

试试这个
eString filePath=context.getFilesDir().getPath().toString()+“/Hello.pdf”
File f=新文件(filePath)

OutputStream文件=新文件OutputStream(f)

谢谢@Rajesh M,我试过了。getAbsolutePath(),但没用。存储/SD卡/似乎没有写入权限。如何更改此设置。我认为您没有创建文件夹,请先创建文件夹,然后写入文件,我已更新代码,请检查iti。我也尝试过此设置,但它没有创建文件夹,因为SD卡文件夹是只读的。我不明白为什么。它拥有dr-xr-xr-x权限。一旦使用真实设备尝试或创建新的仿真器,并将SD卡大小设置为200及以上,感谢@Rajesh M创建新的仿真器。问题在于旧设备SD卡的权限。您是否在清单中提供了android.permission.WRITE_EXTERNAL_STORAGE“
?是的,我已经提供了。在我的AVD的文件浏览器中,它显示了STORAGE/sdcard/,权限类似于dr-xr-xr-x。它没有写入权限,但我不明白为什么会这样。