Search code examples
androidfilesynchronizationstoragegoogle-drive-android-api

Check if mountpoint exists but file does not


I am saving a list of files (absolute paths to a file). It can be of an internal storage or may be external storage.

While syncing those file to the Cloud (i.e. Google Drive), I am checking if file is exists or not at local(File.exists()), and if it does not delete it from the cloud.

But, when the external storage is not mounted, that is causing me to delete the file from the cloud. But, that should not happen. If the mount point is not available it should not check for the file. How can I check that condition?

For example, Files in the internal & external storage looks something like:

  • Internal: /storage/emulated/0/internal.txt
  • External: /storage/4XXX-1234/Android/media/com.example/Documents/external.txt

How can I check if storage is mounted only then check for the file existence?


Solution

  • I found a way to find if file is on the mounted path or not.

    File localFile = new File("/storage/4XXX-1234/Android/media/com.example/Documents/external.txt");
    
    if(Environment.getExternalStorageState(new File(localFile.getAbsolutePath()))
             .equalsIgnoreCase("mounted"))) {
        Log.d(TAG,"File is on the mounted path");
    }