title: Android create file causing java.io.IOException
body: <p>I’m trying to implement a way to create pdf files in my app. Currently I’m using PDF Writer APW for that. I followed the <a href="https://stackoverflow.com/questions/25728927/how-to-add-android-pdf-writer-to-android-project">instruction from jguerinet</a> and pasted all necessary files to the project (as new package). But when I run the code, I always get the below exception. I also tried to use <code>File downloads = Environment.getExternalStorageDirectory();</code> instead of <code>File downloads = Environment.getDataDirectory();</code> wihtout any sucess. Can someone please tell me, what I’m doing wrong?</p>
  
  <pre><code>2018-11-10 12:52:38.843 31238-31238/com.abc.systeminfo I/PDF: Writing file to /data/PDFWriterAPW.pdf
  2018-11-10 12:52:38.845 31238-31238/com.abc.systeminfo W/PDF IOException: java.io.IOException: Permission denied
          at java.io.UnixFileSystem.createFileExclusively0(Native Method)
          at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:281)
          at java.io.File.createNewFile(File.java:1000)
          at com.abc.systeminfo.CreateFile.outputToFile(CreateFile.java:72)
          at com.abc.systeminfo.CreateFile.&lt;init&gt;(CreateFile.java:28)
          at com.abc.systeminfo.ActivityStart$1.onClick(ActivityStart.java:54)
          at android.view.View.performClick(View.java:6256)
          at android.view.View$PerformClick.run(View.java:24701)
          at android.os.Handler.handleCallback(Handler.java:789)
          at android.os.Handler.dispatchMessage(Handler.java:98)
          at android.os.Looper.loop(Looper.java:164)
          at android.app.ActivityThread.main(ActivityThread.java:6541)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
  </code></pre>
  
  <p><strong>CreateFile.java</strong></p>
  
  <pre><code>package com.abc.systeminfo;
  
  import android.os.Environment;
  import android.util.Log;
  
  import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  
  import crl.android.pdfwriter.PDFWriter;
  import crl.android.pdfwriter.StandardFonts;
  import crl.android.pdfwriter.Transformation;
  
  public class CreateFile {
  
      PDFWriter mPDFWriter;
      String pdfcontent;
      String encoding = "ISO-8859-1";
      String fileName = "PDFWriterAPW.pdf";
  
      public CreateFile(String pdfcontent) {
          mPDFWriter = new PDFWriter();
          this.pdfcontent = pdfcontent;   // will be used later instead of String s
          String s = generateHelloWorldPDF();
  
          outputToFile(fileName, s, encoding);
      }
  
      public String generateHelloWorldPDF() {
          mPDFWriter.setFont(StandardFonts.SUBTYPE, StandardFonts.TIMES_ROMAN);
          mPDFWriter.addRawContent("1 0 0 rg\n");
          mPDFWriter.addTextAsHex(70, 50, 12, "68656c6c6f20776f726c6420286173206865782921");
          mPDFWriter.setFont(StandardFonts.SUBTYPE, StandardFonts.COURIER, StandardFonts.WIN_ANSI_ENCODING);
          mPDFWriter.addRawContent("0 0 0 rg\n");
          mPDFWriter.addText(30, 90, 10, "� CRL", Transformation.DEGREES_270_ROTATION);
  
          mPDFWriter.newPage();
          mPDFWriter.addRawContent("[] 0 d\n");
          mPDFWriter.addRawContent("1 w\n");
          mPDFWriter.addRawContent("0 0 1 RG\n");
          mPDFWriter.addRawContent("0 1 0 rg\n");
          mPDFWriter.addRectangle(40, 50, 280, 50);
          mPDFWriter.addText(85, 75, 18, "Code Research Laboratories");
  
          mPDFWriter.newPage();
          mPDFWriter.setFont(StandardFonts.SUBTYPE, StandardFonts.COURIER_BOLD);
          mPDFWriter.addText(150, 150, 14, "http://coderesearchlabs.com");
          mPDFWriter.addLine(150, 140, 270, 140);
  
          int pageCount = mPDFWriter.getPageCount();
          for (int i = 0; i &lt; pageCount; i++) {
              mPDFWriter.setCurrentPage(i);
              mPDFWriter.addText(10, 10, 8, Integer.toString(i + 1) + " / " + Integer.toString(pageCount));
          }
  
          String s = mPDFWriter.asString();
          return s;
      }
  
      private void outputToFile(String fileName, String pdfContent, String encoding) {
          File downloads = Environment.getDataDirectory();
          //File downloads = Environment.getExternalStorageDirectory();
          if (!downloads.exists() &amp;&amp; !downloads.mkdirs())
              throw new RuntimeException("Could not create download folder");
  
          File newFile = new File(downloads, fileName);
          Log.i("PDF", "Writing file to " + newFile);
  
          try {
              newFile.createNewFile();
              try {
                  FileOutputStream pdfFile = new FileOutputStream(newFile);
                  pdfFile.write(pdfContent.getBytes(encoding));
                  pdfFile.close();
              } catch (FileNotFoundException e) {
                  Log.w("PDF FileNotFound ", e);
              }
          } catch (IOException e) {
              Log.w("PDF IOException", e);
          }
      }
  
  }
  </code></pre>
  
  <p><strong>AndroidManifest</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.abc.systeminfo"&gt;
  
      &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:roundIcon="@mipmap/ic_launcher_round"
          android:supportsRtl="true"
          android:theme="@style/AppTheme"&gt;
          &lt;activity android:name=".ActivityStart"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>