How to convert image file to a pdf file in Android

瘦欲@ 提交于 2019-12-06 05:00:52

Just simply do this way it works fine

Document document=new Document();
String dirpath=android.os.Environment.getExternalStorageDirectory().toString();
PdfWriter.getInstance(document,new FileOutputStream(dirpath+"/imagedemo.pdf"));
document.open();
Image im=Image.getInstance(dirpath+"/"+"logo.png");  // Replace logo.png with your image name with extension 
document.add(im);
document.close();

All the examples I've seen directly use the FileOutputStream, did you try without buffer?

PdfWriter.getInstance(document, fileOutputStream);

Found a way to tackle with this problem. I have to do two changes in my code.

  1. Implement DocListener interface in my Activity
  2. Close streams after closing the document

Here are the changes of the second point

try {
    bufferedOutputStream.flush();

    fileOutputStream.flush();

    } catch (IOException ioe) {
        Log.w(TAG, "# Exception caz of flush : " + ioe);
    }

    document.close();

try {
    bufferedOutputStream.close();

    fileOutputStream.close();

    } catch (IOException ioe) {
        Log.w(TAG, "# Exception caz of close : " + ioe);
}     

Still I cannot think the relationship with the Error given in the log and the working code :-/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!