android - file.exists() 为现有文件返回 false(对于任何不同于 pdf 的文件)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21579468/
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
android - file.exists() returns false for existing file (for anything different than pdf)
提问by Goran Horia Mihail
Both files are present on the sdcard, but for whatever reason exists() returns false the the png file.
这两个文件都存在于 SD 卡上,但无论出于何种原因,exists() 都会返回 false png 文件。
//String path = "/mnt/sdcard/Android/data/com.gemoro.toffer/cache/1551619351/0/foto/-921042926.png";
String path = "/mnt/sdcard/Android/data/com.gemoro.toffer/cache/1551619351/0/foto/-1200240592.pdf";
File file2 = new File(path);
if (null != file2)
{
if(file2.exists())
{
LOG.x("file exist");
}
else
{
LOG.x("file does not exist");
}
}
Now, I've look at what's under the hood, what the method file.exists() does actually and this is what it does:
现在,我已经了解了幕后情况,方法 file.exists() 实际做了什么,这就是它的作用:
public boolean exists()
{
return doAccess(F_OK);
}
private boolean doAccess(int mode)
{
try
{
return Libcore.os.access(path, mode);
}
catch (ErrnoException errnoException)
{
return false;
}
}
May it be that the method finishes by throwing the exception and returning false?
可能是该方法通过抛出异常并返回false来完成吗?
If so,
如果是这样的话,
- how can I make this work
- what other options to check if a file exists on the sdcard are available for use?
- 我怎样才能使这项工作
- 还有哪些其他选项可用于检查 SD 卡上是否存在文件?
Thanks.
谢谢。
采纳答案by Alex Chi
1 You need get the permission of device
1 您需要获得设备的许可
Add this to AndroidManifest.xml
将此添加到 AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2 Get the external storage directory
2 获取外部存储目录
File sdDir = Environment.getExternalStorageDirectory();
3 At last, check the file
3 最后,检查文件
File file = new File(sdDir + filename /* what you want to load in SD card */);
if (!file.canRead()) {
return false;
}
return true;
Note: filename is the path in the sdcard, not in root.
注意:filename 是 sdcard 中的路径,而不是 root 中的路径。
For example: you want find
例如:您要查找
/mnt/sdcard/Android/data/com.gemoro.toffer/cache/1551619351/0/foto/-921042926.png
then filename is
然后文件名是
./Android/data/com.gemoro.toffer/cache/1551619351/0/foto/-921042926.png
.
.
回答by Alex Chi
Please try this code. Hope it should helpful for you. I am using this code only. Its working fine for me to find the file is exists or not. Please try and let me know.
请试试这个代码。希望对你有帮助。我仅使用此代码。它对我来说工作正常,可以找到文件是否存在。请尝试让我知道。
File file = new File(path);
if (!file.isFile()) {
Log.e("uploadFile", "Source File not exist :" + filePath);
}else{
Log.e("uploadFile","file exist");
}
回答by user8558196
Check file exist in internal storage
检查内部存储中是否存在文件
Example : /storage/emulated/0/FOLDER_NAME/FILE_NAME.EXTENTION
示例:/storage/emulated/0/FOLDER_NAME/FILE_NAME.EXTENTION
check permission (write storage)
and check file exist or not
public static boolean isFilePresent(String fileName) { return getFilePath(fileName).isFile(); }
get File from the file name
public static File getFilePath(String fileName){ String extStorageDirectory = Environment.getExternalStorageDirectory().toString(); File folder = new File(extStorageDirectory, "FOLDER_NAME"); File filePath = new File(folder + "/" + fileName); return filePath; }
检查权限(写存储)
并检查文件是否存在
public static boolean isFilePresent(String fileName) { return getFilePath(fileName).isFile(); }
从文件名中获取文件
public static File getFilePath(String fileName){ String extStorageDirectory = Environment.getExternalStorageDirectory().toString(); File folder = new File(extStorageDirectory, "FOLDER_NAME"); File filePath = new File(folder + "/" + fileName); return filePath; }
回答by GMG
Check that USB Storage is not connected to the PC. Since Android device is connected to the PC as storage the files are not available for the application and you get FALSE to File.Exists().
检查 USB 存储设备是否未连接到 PC。由于 Android 设备作为存储连接到 PC,因此文件不可用于应用程序,并且 File.Exists() 为 FALSE。