2011-12-27 57 views

回答

1

使用FileOutputStream及其write(byte[])方法

#假如你已经被数据内容是准备在PDF

writen也有可用于Android

一些PDF作家API见

+0

您好Jigar,我尝试这个代码FileOutputStream中fileos =新FileOutputStream中( “Hello.pdf”); 但是当我试图打开这个文件时,我收到一个错误,说Adobe Reader无法打开Hello.pdf,因为它不是受支持的文件类型,或者是因为文件已损坏...... – Taruni 2011-12-27 12:16:33

+0

_假设您有数据准备在PDF_中写入的内容,请参阅更新 – 2011-12-27 12:21:14

2

试试这个代码,它为我工作。

File dir = Environment.getExternalStorageDirectory(); 

File assist = new File("/mnt/sdcard/Sample.pdf"); 
try { 
    InputStream fis = new FileInputStream(assist); 

    long length = assist.length(); 
    if (length > Integer.MAX_VALUE) { 
     Log.e("Sorry! Your given file is too large.","cannnottt readddd"); 
    } 
    byte[] bytes = new byte[(int)length]; 
    int offset = 0; 
    int numRead = 0; 
    while (offset < bytes.length && (numRead=fis.read(bytes, offset, bytes.length-offset)) >= 0) { 
     offset += numRead; 
    } 

    File data = new File(dir,"mydemo.pdf"); 
    OutputStream op = new FileOutputStream(data); 
    op.write(bytes); 

创建的mydemo.pdf将存储在您的SD卡上。

+0

上述代码中的更正为File assist = new File(“/ mnt/sdcard”); – Taruni 2011-12-28 06:40:14

+0

当我试图打开这个文件时,我是一个错误,说Adobe Reader无法打开该文件,因为它不是受支持的文件类型,或者是因为文件已损坏。 – Taruni 2011-12-28 06:45:43

0

您可以使用Common IO库来帮助简单地从文件转换为字节数组或从字节数组转换为文件。

File mFile = new File(filePath); 
    FileUtils.writeByteArrayToFile(mFile, yourArray); 
相关问题