I am trying to download a file from the net and write it to the SD card. I have the WRITE_EXTERNAL_STORAGE permission enabled. The code fails here:
OutputStream output = new FileOutputStream(filePath);
09-15 19:40:31.630: WARN/System.err(7933): java.io.FileNotFoundException: /sdcard/artoo/Customerv08.apk
09-15 19:40:31.630: WARN/System.err(7933): at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:231)
09-15 19:40:31.630: WARN/System.err(7933): at java.io.FileOutputStream.<init>(FileOutputStream.java:96)
09-15 19:40:31.630: WARN/System.err(7933): at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
09-15 19:40:31.630: WARN/System.err(7933): at java.io.FileOutputStream.<init>(FileOutputStream.java:147)
09-15 19:40:31.640: WARN/System.err(7933): at com.artoo.settings.InstallerActivity$ProgressDialogAsyncTask.doInBackground(InstallerActivity.java:148)
09-15 19:40:31.640: WARN/System.err(7933): at com.artoo.settings.InstallerActivity$ProgressDialogAsyncTask.doInBackground(InstallerActivity.java:1)
09-15 19:40:31.640: WARN/System.err(7933): at android.os.AsyncTask$2.call(AsyncTask.java:185)
09-15 19:40:31.640: WARN/System.err(7933): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:256)
09-15 19:40:31.640: WARN/System.err(7933): at java.util.concurrent.FutureTask.run(FutureTask.java:122)
09-15 19:40:31.640: WARN/System.err(7933): at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:648)
09-15 19:40:31.640: WARN/System.err(7933): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:673)
09-15 19:40:31.651: WARN/System.err(7933): at java.lang.Thread.run(Thread.java:1060)
09-15 19:40:31.670: WARN/InputManagerService(53): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@43868480
I am trying to write the file and it doesn’t exists from before. Any ideas on how to do this?
You are getting that error because the file specified cannot be opened or does not exist. It’s simple as that.
I always create a file before and use a bufferedstream to write to it, ex :