Java ENOENT(没有这样的文件或目录)当有文件时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19992706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ENOENT (No such file or directory) When There Is A File There
提问by Steve Smith
I am trying to share A PNG with ShareActionProvider in Android. When I open the PNG to the Uri, it says there is no file found.
open failed: ENOENT (No such file or directory)
even though I have gone into the file system and have seen it myself. I have tried it on my phone and the AVD with the save error. I have looked around but have found no answers. Any help would be much appreciated.
我正在尝试在 Android 中与 ShareActionProvider 共享 A PNG。当我打开 PNG 到 Uri 时,它说没有找到文件。
open failed: ENOENT (No such file or directory)
即使我已经进入文件系统并亲自看过它。我已经在我的手机和 AVD 上尝试过,但保存错误。我环顾四周,但没有找到答案。任何帮助将非常感激。
Here is where I try to open the file:
这是我尝试打开文件的地方:
File file = new File(getFilesDir()+"wifiqr/", "QRCode.png");
file.setReadable(true, false);
Uri uri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM,uri);
provider.setShareIntent(intent);
If it helps here is where I save it:
如果有帮助,这里是我保存它的地方:
String fileName = getFilesDir()+"/wifiqr/" + "QRCode.png";
etSSID.setText(fileName);
OutputStream stream = null;
try {
stream = new FileOutputStream(fileName);
bmp.compress(Bitmap.CompressFormat.PNG, 80, stream);
stream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Finally the error log:
最后是错误日志:
11-15 02:34:43.243 594-892/com.android.mms E/Mms/media: IOException caught while opening or reading stream
java.io.FileNotFoundException: /data/data/com.frostbytedev.wifiqr/fileswifiqr/QRCode.png: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:416)
at java.io.FileInputStream.<init>(FileInputStream.java:78)
at java.io.FileInputStream.<init>(FileInputStream.java:105)
at android.content.ContentResolver.openInputStream(ContentResolver.java:447)
at com.android.mms.model.MediaModel.initMediaSize(MediaModel.java:235)
at com.android.mms.model.MediaModel.<init>(MediaModel.java:74)
at com.android.mms.model.RegionMediaModel.<init>(RegionMediaModel.java:36)
at com.android.mms.model.RegionMediaModel.<init>(RegionMediaModel.java:31)
at com.android.mms.model.ImageModel.<init>(ImageModel.java:73)
at com.android.mms.ui.SlideshowEditor.changeImage(SlideshowEditor.java:163)
at com.android.mms.data.WorkingMessage.internalChangeMedia(WorkingMessage.java:640)
at com.android.mms.data.WorkingMessage.changeMedia(WorkingMessage.java:588)
at com.android.mms.data.WorkingMessage.setAttachment(WorkingMessage.java:453)
at com.android.mms.ui.ComposeMessageActivity.addImage(ComposeMessageActivity.java:3150)
at com.android.mms.ui.ComposeMessageActivity.addAttachment(ComposeMessageActivity.java:3291)
at com.android.mms.ui.ComposeMessageActivity.access00(ComposeMessageActivity.java:167)
at com.android.mms.ui.ComposeMessageActivity.run(ComposeMessageActivity.java:3236)
at com.android.mms.ui.AsyncDialog$ModalDialogAsyncTask.doInBackground(AsyncDialog.java:129)
at com.android.mms.ui.AsyncDialog$ModalDialogAsyncTask.doInBackground(AsyncDialog.java:84)
at android.os.AsyncTask.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
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:400)
... 24 more
回答by Jason C
In your save code you have:
在您的保存代码中,您有:
String fileName = getFilesDir()+"/wifiqr/" + "QRCode.png";
In your load code you have no leading slash before "wifiqr/":
在您的加载代码中,“wifiqr/”之前没有前导斜杠:
File file = new File(getFilesDir()+"wifiqr/", "QRCode.png");
It seems you meant to put a "/" before "wifiqr/" in your load code. Your save code implies that getFilesDir()
may not already have a trailing slash on it.
您似乎打算在加载代码中的“wifiqr/”之前放置一个“/”。您的保存代码意味着它getFilesDir()
可能还没有尾部斜杠。
The name of the file it is trying to open is even printed in your log:
它尝试打开的文件的名称甚至会打印在您的日志中:
/data/data/com.frostbytedev.wifiqr/fileswifiqr/QRCode.png
It doesn't seem like you expect to have a directory named "fileswifiqr".
您似乎不希望有一个名为“fileswifiqr”的目录。
The missing link in your troubleshooting attempt was: While you knew the file you were intendingto open existed, you still needed to make sure that your code was actually opening the file you were intending to open.
在您的故障排除尝试缺失的一环是:当你知道你的文件打算以开放的存在,你还需要确保你的代码实际上是打开你打算打开该文件。
回答by ianhanniballake
The getFilesDir()
is private to that specific app (i.e., other apps cannot read it) as explained in openFileOutput(which is the same directory getFilesDir
returns as per its documentation).
如openFileOutput 中所述getFilesDir()
,该特定应用程序是私有的(即,其他应用程序无法读取它)(根据其文档,该目录返回的目录相同)。getFilesDir
If you are attempting to share files across applications, you follow the Sharing Files training guideto ensure that other applications can access your files.
如果您尝试跨应用程序共享文件,请遵循共享文件培训指南以确保其他应用程序可以访问您的文件。