下载管理器 getFilesDir()

DownloadManager getFilesDir()

我需要将文件下载到设备的内存中。我是这样做的:

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

文件加载成功。我需要检查此文件是否存在,这样我就不必再次下载它了:

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

检查无效,文件总是重新加载。 我也尝试获取 MediaPlayer()

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

这也行不通... 我做错了什么?

您正在将文件保存到设备内部存储器,但在尝试获取您正在搜索的文件时,您正在应用程序的私有内存中。这与下载 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

文件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.