Tôi muốn kiểm tra xem tệp đã tồn tại trong thẻ sd android hay chưa. Tôi đang cố gắng nó ra với việc tạo ra một tập tin bằng cách sử dụng đường dẫn tuyệt đối và kiểm tra với file.exists()
nhưng nó không hoạt động. URL cho tệp là "file:///mnt/sdcard/book1/page2.html"
và tệp không tồn tại. Nhưng bằng cách nào đó, file.exists()
không hiển thị giống nhau.Cách kiểm tra xem tệp có tồn tại trong một thư mục trong thẻ sd
7
A
Trả lời
46
File extStore = Environment.getExternalStorageDirectory();
File myFile = new File(extStore.getAbsolutePath() + "/book1/page2.html");
if(myFile.exists()){
...
}
này nên làm việc.
+0
Cảm ơn rất nhiều! Những công việc này!! – working
+0
Chào mừng bạn! Hãy đánh dấu nó được chấp nhận, nếu nó giải quyết được vấn đề của bạn, cảm ơn. –
0
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
1
Bạn có thể kiểm tra như sau:
File file = new File(getExternalCacheDirectory(), "mytextfile.txt");
if (file.exists()) {
//Do action
}
0
Đỗ một cái gì đó như thế này:
File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, "your/file/path");
if(yourFile.exists())
{
}
7
Hãy thử như thế này:
File file = new File(Environment.getExternalStorageDirectory() + "/book1/page2.html");
if (file.exists()) {
/*...*/
}
Ngoài ra hãy chắc chắn rằng bạn có:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
trong file manifest của bạn.
0
String filepath = getFilesDir().getAbsolutePath();
String FileName = "Yourfilename" ;
File FileMain = new File(filepath, FileName);
if (FileMain.exists()){
do somthing here
}else{}
0
File file = new File(path+filename);
if (file.exists())
{
//Do something
}
kiểm tra, điều này sẽ làm việc
trùng lặp có thể xảy ra của [Kiểm tra nếu tập tin tồn tại trên thẻ SD trên Android] (https://stackoverflow.com/questions/7697650/check-if-file -exists-on-sd-card-on-android) –