android - file.exists() returns false for existing file (for anything different than pdf)

前端 未结 4 779
感动是毒
感动是毒 2020-12-31 05:19

Both files are present on the sdcard, but for whatever reason exists() returns false the the png file.

//String path = \"/mnt/sdcard/Android/data/com.gemoro.         


        
相关标签:
4条回答
  • 2020-12-31 05:36

    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");
    }
    
    0 讨论(0)
  • 2020-12-31 05:44

    1 You need get the permission of device

    Add this to AndroidManifest.xml

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

    2 Get the external storage directory

    File sdDir = Environment.getExternalStorageDirectory();
    

    3 At last, check the file

    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.

    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
    

    .

    0 讨论(0)
  • 2020-12-31 05:51

    Check file exist in internal storage

    Example : /storage/emulated/0/FOLDER_NAME/FILE_NAME.EXTENTION

    1. check permission (write storage)

    2. and check file exist or not

      public static boolean isFilePresent(String fileName) { return getFilePath(fileName).isFile(); }

    3. 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; }

    0 讨论(0)
  • 2020-12-31 05:52

    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().

    0 讨论(0)
提交回复
热议问题