Android 如何检查文件是否存在于sd卡的目录中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11292359/
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
How to check if a file exists in a directory in sd card
提问by working
I want to check whether a given file exists in android sd card. I am trying it out with creating a file using the absolute path and checking with file.exists()
but its not working. The URL for the file is "file:///mnt/sdcard/book1/page2.html"
and the file does exist. But somehow file.exists()
isn't showing the same.
我想检查android sd卡中是否存在给定的文件。我正在尝试使用绝对路径创建文件并检查file.exists()
但它不起作用。该文件的 URL 是 "file:///mnt/sdcard/book1/page2.html"
并且该文件确实存在。但不知何故file.exists()
不显示相同。
回答by Adam L. Mónos
File extStore = Environment.getExternalStorageDirectory();
File myFile = new File(extStore.getAbsolutePath() + "/book1/page2.html");
if(myFile.exists()){
...
}
This should work.
这应该有效。
回答by Caner
Try like this:
像这样尝试:
File file = new File(Environment.getExternalStorageDirectory() + "/book1/page2.html");
if (file.exists()) {
/*...*/
}
Also make sure you have:
还要确保你有:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
in your manifest file.
在您的清单文件中。
回答by Aamirkhan
You can check as follows:
您可以按以下方式检查:
File file = new File(getExternalCacheDirectory(), "mytextfile.txt" );
if (file.exists()) {
//Do action
}
回答by Ankitkumar Makwana
String filepath = getFilesDir().getAbsolutePath();
String FileName = "Yourfilename" ;
File FileMain = new File(filepath, FileName);
if (FileMain.exists()){
do somthing here
}else{}
回答by Arunprabu
File file = new File(path+filename);
if (file.exists())
{
//Do something
}
checked, this will work
检查,这将工作
回答by sunil
File logFile = new File(
android.os.Environment.getExternalStorageDirectory()
+ "/book1/", "page2.tml");
if (logFile.exists())
System.out.println("file exists");
else
System.out.println("file does not exist
回答by Vipul Purohit
Do something like this :
做这样的事情:
File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, "your/file/path");
if(yourFile.exists())
{
}