Note that there are some explanatory texts on larger screens.

plurals
  1. POFileOutputStream : FileNotFoundException when attempting to instantiate
    primarykey
    data
    text
    <p>Trying to create and write to a file, but i get a <code>FileNotFoundException</code> every time, here is the method i am using:</p> <pre><code>public void saveFileAsPRN(Context context){ byte[] dataFile = getPrintableFileData(); String filename = "TestPrn.prn"; // instantiate a file object using the path File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), filename); Log.e(TAG, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString()); //determine if the media is mounted with read &amp; write access String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { Log.e(TAG, "media mounted"); //good }else{ Log.e(TAG, "media NOT mounted"); //bad } //create directory if it does not exist //the default Download directory should always exist if (!file.mkdirs()) { Log.e(TAG, "Directory not created"); } // determine if the file exists, create it if it does not if(!file.exists()){ try { Log.e(TAG, "File does not exist, creating.."); file.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else{ Log.e(TAG, "File Exists"); } //this makes the blank file visible in the file browser MediaScannerConnection.scanFile(context, new String[]{Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "/" + filename}, null, null); //create output stream - send data; saving to file OutputStream out = null; try { FileOutputStream fos = null; fos = new FileOutputStream(file); // &lt;---- CRASHES HERE; FileNotFoundException out = new BufferedOutputStream(fos); out.write(dataFile); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { out.flush(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } </code></pre> <p>A <code>FileNotFoundException</code> is raised on the following line: </p> <pre><code>fos = new FileOutputStream(file); // &lt;---- CRASHES HERE; </code></pre> <p>The directory Exists, and a blank file is created in the target directory (visible by browsing target folder on PC).</p> <p>Calling the method <code>canWrite()</code> on the <code>File</code> object returns true - i have write access.</p> <p>The manifest contains: <code>android.permission.WRITE_EXTERNAL_STORAGE</code>"</p> <p>So i'm out of ideas, i see several people have similar issues, but i cant find an answer.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload