gpt4 book ai didi

java - 下载管理器 getFilesDir()

转载 作者:行者123 更新时间:2023-11-30 10:31:36 26 4
gpt4 key购买 nike

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

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));

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

最佳答案

您正在将文件保存到设备内部存储器,但在尝试获取正在搜索的应用程序私有(private)内存中的文件时。这与下载本地化不同

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

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.

关于java - 下载管理器 getFilesDir(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43122738/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com