Search code examples
javaandroidandroid-download-manager

DownloadManager getFilesDir()


I need to download the file to the internal memory of the device. I do it this way:

Uri downloadUri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
request.setDestinationInExternalFilesDir(getContext(),getContext().getFilesDir().getAbsolutePath(), name);

The file loads successfully. I need to check the presence of this file so I do not have to download it again:

File file = new File(getContext().getFilesDir().getAbsolutePath(), name);
if (!file.exists()) {
   ...
}

The check does not work, the file always loads again. I also try to get a file for MediaPlayer()

String path = "file://" + getContext().getFilesDir().getAbsolutePath() + File.separator + name;
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(getContext(), Uri.parse(path));

This also does not work... What am I doing wrong?


Solution

  • you are saving the file to device internal memory but while trying to get the file you are searching in app's private memory. Which is different from download localtion

    setDestinationInExternalPublicDir

    DownloadManager.Request setDestinationInExternalPublicDir (String dirType, String subPath) Set the local destination for the downloaded file to a path within the public external storage directory (as returned by getExternalStoragePublicDirectory(String)).

    The downloaded file is not scanned by MediaScanner. But it can be made scannable by calling allowScanningByMediaScanner().

    getFilesDir

    File getFilesDir ()

    Returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int) are stored.

    The returned path may change over time if the calling app is moved to an adopted storage device, so only relative paths should be persisted.

    No additional permissions are required for the calling app to read or write files under the returned path.