Android - only file with an opened error

I am new to Android development and trying to make some input / output files. Whenever I run this block of code:

File meta = new File(context.getAppContext().getFilesDir(),"meta"); meta.mkdirs(); File dir = new File(meta,"subdir"); File imageFile = new File(dir,"filename"); Log.d("test",imageFile.getAbsolutePath()); FileOutputStream outputStream = new FileOutputStream(imageFile); 

I get this error:

  java.io.FileNotFoundException: /data/data/com.example.android.networkusage/files/meta/Greg and The Morning Buzz/artwork30.jpg: open failed: ENOENT (No such file or directory) at libcore.io.IoBridge.open(IoBridge.java:406) at java.io.FileOutputStream.<init>(FileOutputStream.java:88) at java.io.FileOutputStream.<init>(FileOutputStream.java:73) at com.example.android.networkusage.Podcast.downloadArtworkFromUrl(Podcast.java:117) at com.example.android.networkusage.Podcast.<init>(Podcast.java:93) at com.example.android.networkusage.JSONParser.parse(JSONParser.java:113) at com.example.android.networkusage.NetworkActivity.loadXmlFromNetwork(NetworkActivity.java:240) at com.example.android.networkusage.NetworkActivity.access$100(NetworkActivity.java:65) at com.example.android.networkusage.NetworkActivity$DownloadXmlTask.doInBackground(NetworkActivity.java:203) at com.example.android.networkusage.NetworkActivity$DownloadXmlTask.doInBackground(NetworkActivity.java:198) at android.os.AsyncTask$2.call(AsyncTask.java:264) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) at java.util.concurrent.FutureTask.run(FutureTask.java:137) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) at java.lang.Thread.run(Thread.java:856) Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory) at libcore.io.Posix.open(Native Method) at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110) at libcore.io.IoBridge.open(IoBridge.java:390) ... 16 more 

The log even prints the file path as directed, so the file must exist! Why is this happening?

In addition, my application has internal and external write permissions.

+6
source share
2 answers

One of the subdirectories ( subdir ) and the file does not exist. The code must call createNewFile() to create the file. You also need to create the subdir directory by calling mkdirs() .

 File meta = new File(context.getAppContext().getFilesDir(),"meta") meta.mkdirs(); File dir = new File(meta, "subdir"); dir.mkdirs(); //added File imageFile = new File(dir, "filename.txt"); imageFile.createNewFile(); //added FileOutputStream outputStream = new FileOutputStream(imageFile); 

I do not think that the File constructor actually creates a file in the file system. The documentation states:

Instances of this class may or may not denote the actual file system object, such as a file or directory. If it denotes such an object, then this object is in the section. The partition is the operating system part of the repository for the file system. A single storage device (for example, a physical drive, flash memory, CD-ROM) can contain several partitions. The object, if any, will be in the section named by some ancestor of the absolute form of this path.

+6
source

this is a code snippet from a project I'm working on now:

from inside an action / context:

 File dir = getDir("Foler", 0); // i think your problem is here File file = new File(dir,"File.bin"); 

creating a directory in android is different from java on a regular computer, try creating your directory using getDir () instead of a new file (...). mkdirs () and make sure it is inside the context (after activity is created)

I do not believe that I am the best to answer this question, since I am also new to android, but maybe this helps

so the final code could be like that

 File dir = getDir("subdir",0); File imageFile = new File(dir, "filename.txt"); imageFile.createNewFile(); FileOutputStream outputStream = new FileOutputStream(imageFile); 

I don't know about creating subdirectories, I still haven't tried it

+1
source

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


All Articles