1

In my app After user clicks on a button ,the download manager starts to download a file from internet and saving it to the internal sd card using this code:

void startDownload()
    {
        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).mkdirs();        
        download_req.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
        .setAllowedOverRoaming(false)
        .setTitle(PersianReshape.reshape("Downloading"))
        .setDescription(PersianReshape.reshape("currently downloading the file..."))
        .setDestinationInExternalPublicDir(Environment.getExternalStorageDirectory().getAbsolutePath() , packageId + ".sqlite");
        download_ID = mgr.enqueue(download_req);
    }

After it is downloaded, I plan to check its existance everytime app runs with this code:

String DatabaseAddress =
Environment.getExternalStorageDirectory().getAbsolutePath() +
"/ee.sqlite";
File file = new File(DatabaseAddress);
Log.d("PATH File: ", DatabaseAddress);
if (file.exists()){
Toast.makeText(getApplicationContext(), "found", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "not found", Toast.LENGTH_SHORT).show();
}

Now when I run this code it returns "not found" message whereas the file is already there (I checked its existance using a file manager).

the device I test on is nexus 7 and path used in saving the download file is: /storage/emulated/0/ee.sqlite

ee.sqlite is the filename of downloaded file.

/storage/emulated/0/ is the default path returned by app

Permissions added to manifest for this code are:

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

Q: Why does file.exists() returns false when there is a file?

UPDATE: WHEN I USE HARDCODE

File temp = new File("/sdcard/storage/emulated/0/", "ee.sqlite");

it works but when I use Environment.getExternalStorageDirectory().getPath() it doesn't.

Nima Sakhtemani
  • 1,119
  • 2
  • 10
  • 21

2 Answers2

1

Just initialize the File like this:

Updated: This is just because of the separator missing there:

 public static File getExternalStorageDirectory() {
    return EXTERNAL_STORAGE_DIRECTORY;
}

and EXTERNAL_STORAGE_DIRECTORY is:

private static final File EXTERNAL_STORAGE_DIRECTORY
        = getDirectory("EXTERNAL_STORAGE", "/sdcard");

static File getDirectory(String variableName, String defaultPath) {
    String path = System.getenv(variableName);
    return path == null ? new File(defaultPath) : new File(path);
}

String baseDir = EXTERNAL_STORAGE_DIRECTORY ;
String fileName = "ee.sqlite";

// Not sure if the / is on the path or not
File file = new File(baseDir + File.separator + fileName);
if (file.exists()) {
        Toast.makeText(getApplicationContext(), "found", Toast.LENGTH_SHORT).show(); 
    }
else{
        Toast.makeText(getApplicationContext(), "not found", Toast.LENGTH_SHORT).show();
    }

2nd Possibilites: As you are running it in emulator you have to make sure you have external storage enabled Like this:

In Eclipse, go to Window > Android SDK and AVD Manager. Select the appropriate AVD and then click Edit.

Window - Android SDK and AVD Manager

Make sure you have SD card support enabled. If you do not, click the "New" button and select the "SD Card Support" option.

Edit screen

Avijit
  • 3,834
  • 4
  • 33
  • 45
1

Finally got it to work by changing the File object.

The solution is to change the File object to the following:

File file = new File(Environment.getExternalStorageDirectory().getPath() + dirName + File.separator , fileName);

where dirName is:

String dirName = Environment.getExternalStorageDirectory().getAbsolutePath();

and fileName is:

String fileName = "ee.sqlite";

so I could easily check whether file exist or not using my criteria function.

if (file.exists()){
            Toast.makeText(getApplicationContext(), "found", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(getApplicationContext(), "not found", Toast.LENGTH_SHORT).show();
        }

I like to thank @andru for helping me.

Nima Sakhtemani
  • 1,119
  • 2
  • 10
  • 21
  • That doesn't seem to make much sense, you're including two times the absolute path to the external storage directory. – RedGlyph Mar 14 '15 at 15:25