[英]Android - error opening file just created
I'm new to android developement and trying to do some file IO. 我是android developpement的新手,并尝试做一些文件IO。 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 out the file's path as directed, so the file must exist! 日志甚至按照指示打印出文件的路径,因此文件必须存在! Why is this happening? 为什么会这样?
Also, my app has internal and external write priviledges. 此外,我的应用程序具有内部和外部写入权限。
One of the sub-directories ( subdir
) and file do not exist. 其中一个子目录( subdir
)和文件不存在。 The code should call createNewFile()
to actually create the file. 代码应该调用createNewFile()
来实际创建文件。 Also the subdir
directory needs to be made by calling mkdirs()
. 还需要通过调用mkdirs()
来生成subdir
目录。
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 believe the File
constructor actually creates the file on the file system. 我不相信File
构造函数实际上在文件系统上创建文件。 The documentation states: 文件说明:
Instances of this class may or may not denote an actual file-system object such as a file or a directory. 此类的实例可能表示也可能不表示实际的文件系统对象,例如文件或目录。 If it does denote such an object then that object resides in a partition. 如果它确实表示这样的对象,则该对象驻留在分区中。 A partition is an operating system-specific portion of storage for a file system. 分区是文件系统的特定于操作系统的存储部分。 A single storage device (eg a physical disk-drive, flash memory, CD-ROM) may contain multiple partitions. 单个存储设备(例如,物理磁盘驱动器,闪存,CD-ROM)可以包含多个分区。 The object, if any, will reside on the partition named by some ancestor of the absolute form of this pathname. 该对象(如果有)将驻留在由此路径名的绝对形式的某个祖先命名的分区上。
this is a snippet of code from a project i'm working on right now : 这是我正在进行的项目的一小段代码:
from inside an activity/context : 来自活动/背景:
File dir = getDir("Foler", 0); // i think your problem is here
File file = new File(dir,"File.bin");
creating directory in android is different than java on normal computer, try to create your directory using getDir() instead of new File(...).mkdirs(), and make sure it is from inside a context (after the activity is created) 在android中创建目录与普通计算机上的java不同,尝试使用getDir()而不是新文件(...)。mkdirs()创建目录,并确保它来自上下文(在创建活动之后) )
i dont believe i'm the best one to answer this question since i'm new to android as well, but maybe this helps 我不相信我是回答这个问题的最佳人选,因为我也是Android的新手,但也许这会有所帮助
so final code maybe like this 所以最终的代码可能是这样的
File dir = getDir("subdir",0);
File imageFile = new File(dir, "filename.txt");
imageFile.createNewFile();
FileOutputStream outputStream = new FileOutputStream(imageFile);
i dont know about making nested directories, i didnt try it up till now 我不知道制作嵌套目录,我直到现在都没有尝试过
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.