FileNotFoundException + Write file to SD card

I am trying to download a file from the network and write it to an SD card. I have permission WRITE_EXTERNAL_STORAGE. The code here does not work:

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 a file and it does not exist before. Any ideas on how to do this?

+3
source share
2 answers

You get this error because the specified file cannot be opened or does not exist. It's simple.

I always create a file before and use a buffer stream for it, for example:

File file = new File(path, name);
file.createNewFile();
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file), BUFFER);
+8
source

check your permission in the manifest file, man,

grant your application permission WRITE_EXTERNAL_STORAGE

+2
source

Source: https://habr.com/ru/post/1764895/


All Articles