0

I'm trying to save a photo made by camera in an Android application. I use an AsyncTask class to do the job, however in onBackground function an exception is thrown - FileNotFoundException. It happens even though I have both read and write permissions for external storage in manifest.

private class SaveImageTask extends AsyncTask<byte[], Void, String> {

    @Override
    protected String doInBackground(byte[]... data) {
        FileOutputStream outStream = null;
        String url = "example";
        try {       
            System.out.println("x");
            File dir = new File (Environment.getExternalStorageDirectory() + "/camtest/");
            System.out.println(dir);
            if(!dir.exists()) dir.mkdir();              
            Locale locale = new Locale("en");
            String fileName = String.format(locale, "%d.jpg", System.currentTimeMillis());          
            File outFile = new File(dir, fileName);
            System.out.println(outFile.getAbsolutePath());
            outStream = new FileOutputStream(outFile);              
            outStream.write(data[0]);
            outStream.flush();
            outStream.close();          
            System.out.println("x");
            refreshGallery(outFile);        
            Toast.makeText(getApplicationContext(), "try", Toast.LENGTH_LONG).show();
            url = outFile.getAbsolutePath();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("File not found");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO exception");
        } finally {
        }
        return url;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        locationToSave.setPhotoURL(result);
        Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();

    }
}

My output log is the following:

01-02 16:53:12.692: I/System.out(16460): x
01-02 16:53:12.692: I/System.out(16460): /mnt/sdcard/camtest
01-02 16:53:12.692: I/System.out(16460): /mnt/sdcard/camtest/1420213992701.jpg
01-02 16:53:12.722: I/System.out(16460): File not found

So evidently it jumps to catch block when trying to write the file. The path looks good, too, in my opinion.

Any ideas?

EDIT: stack trace

01-02 17:18:48.300: W/System.err(18516): java.io.FileNotFoundException:     /mnt/sdcard/camtest/1420215528309.jpg: open failed: EACCES (Permission denied)
01-02 17:18:48.300: W/System.err(18516):    at libcore.io.IoBridge.open(IoBridge.java:406)
01-02 17:18:48.300: W/System.err(18516):    at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
01-02 17:18:48.300: W/System.err(18516):    at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
01-02 17:18:48.300: W/System.err(18516):    at     michal.myapp.activities.CameraActivity$SaveImageTask.doInBackground(CameraActivity.java:244)
01-02 17:18:48.300: W/System.err(18516):    at michal.myapp.activities.CameraActivity$SaveImageTask.doInBackground(CameraActivity.java:1)
01-02 17:18:48.300: W/System.err(18516):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
01-02 17:18:48.300: W/System.err(18516):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-02 17:18:48.300: W/System.err(18516):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-02 17:18:48.300: W/System.err(18516):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
01-02 17:18:48.300: W/System.err(18516):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-02 17:18:48.300: W/System.err(18516):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-02 17:18:48.300: W/System.err(18516):    at java.lang.Thread.run(Thread.java:864)
01-02 17:18:48.300: W/System.err(18516): Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
01-02 17:18:48.310: W/System.err(18516):    at libcore.io.Posix.open(Native Method)
01-02 17:18:48.310: W/System.err(18516):    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
01-02 17:18:48.310: W/System.err(18516):    at libcore.io.IoBridge.open(IoBridge.java:390)
01-02 17:18:48.310: W/System.err(18516):    ... 11 more
user4359659
  • 149
  • 2
  • 4
  • 18
  • why do you return __url__ twice? – Kyle Emmanuel Jan 02 '15 at 16:06
  • yes, I forgot to delete the second one. but it's irrelevant, because the program never reaches the end of try block anyway – user4359659 Jan 02 '15 at 16:07
  • Can you add the Stacktrace of the exception? There might be a permission problem: [JavaDoc](http://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html#FileOutputStream%28java.io.File%29). – Tom Jan 02 '15 at 16:13
  • Strange, seems you might be right with permissions. See my edit. – user4359659 Jan 02 '15 at 16:20
  • What other permissions do I need than ? – user4359659 Jan 02 '15 at 16:22
  • 1
    Check this, it might help: http://stackoverflow.com/questions/8854359/exception-open-failed-eacces-permission-denied-on-android – Tom Jan 02 '15 at 16:24
  • If it helped and you could fix your problem, then you should write an answer (you can accept it in a few days) to help other readers of your question. – Tom Jan 02 '15 at 16:30

1 Answers1

1

The permission:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

should be declared outside of the application tag in Android Manifest. Like in this thread:

Exception 'open failed: EACCES (Permission denied)' on Android

Community
  • 1
  • 1
user4359659
  • 149
  • 2
  • 4
  • 18