7

特定のファイルがAndroid SDカードに存在するかどうかを確認したい。絶対パスを使用してファイルを作成し、チェックして試していますfile.exists()が、機能しません。ファイルの URL は "file:///mnt/sdcard/book1/page2.html"で、ファイルは存在します。しかし、どういうわけかfile.exists()同じものを示していません。

4

7 に答える 7

51
File extStore = Environment.getExternalStorageDirectory();
File myFile = new File(extStore.getAbsolutePath() + "/book1/page2.html");

if(myFile.exists()){
    ...
}

これはうまくいくはずです。

于 2012-07-02T11:09:00.820 に答える
7

このようにしてみてください:

File file = new File(Environment.getExternalStorageDirectory() + "/book1/page2.html");
if (file.exists()) {
    /*...*/
}

また、以下があることを確認してください。

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

あなたのマニフェストファイルに。

于 2012-07-02T11:10:08.527 に答える
1

次のように確認できます。

  File file = new File(getExternalCacheDirectory(), "mytextfile.txt" );
  if (file.exists()) {
      //Do action
   }
于 2012-07-02T11:08:31.900 に答える
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
于 2012-07-02T11:07:30.153 に答える
0

このようなことをしてください:

File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, "your/file/path");

if(yourFile.exists())
{

}
于 2012-07-02T11:09:50.897 に答える
0
String filepath = getFilesDir().getAbsolutePath(); 
String FileName = "Yourfilename" ;
File FileMain = new File(filepath, FileName); 
if (FileMain.exists()){ 
do somthing here                     
}else{}
于 2012-07-02T11:11:09.913 に答える