我正在尝试将文件保存到下载文件夹。我获取目录并使用如下方式保存文件:
File exportDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "");
if (!exportDir.exists()) {
exportDir.mkdirs();
}
File file = new File(exportDir, "filename.csv");
try {
if (file.exists()) {
// if the file exists create new file will do nothing. We want to
// overwrite contents every time
file.delete();
}
boolean created = file.createNewFile();
if (!created) {
Log.d("", "Failed to create file");
}
// .. write to file
MediaScannerConnection.scanFile(activity, new String[]{exportDir.getPath()}, null, null);
} catch (Exception sqlEx) {
Log.e("MainActivity", sqlEx.getMessage(), sqlEx);
}
我已经在三种设备上测试过了:
- 一加手机 3(安卓 8.0.0)
- 蓝光高级 5.0(Android 5.1)
- 三星 Galaxy S3(Android 4.4.4)
在所有设备上,如果我导航到/storage/sdcard0/Download 或类似的东西,我可以在那里看到我的文件。但是,如果我尝试打开 native “下载”应用程序,我无法在 Blu 或 Galaxy 上看到该文件,但我可以在我的 OnePlus 3 上看到它。
我曾尝试重启手机以触发媒体扫描,并尝试使用 MediaScannerConnection.scanFile
扫描文件和文件夹,但没有成功。根据 FileManager,该文件是 -rw-rw-----
就像该文件夹中的大多数其他文件一样,这里发生了什么?
我的目标是将文件保存到一个合理的位置,用户可以轻松找到它并将其从手机中取出。 Downloads 文件夹是否有特殊规则?
如有任何建议,我们将不胜感激。
最佳答案
Does the Downloads folder have special rules?
外部存储上的 Downloads/
文件夹没有。下载应用程序可以。经典的下载应用程序适用于通过 DownloadManager
下载的文件,但设备制造商可以根据需要对其进行修改。
My goal is to save the file to a logical place where the user can easily find it and pull it off of their phone.
Downloads/
可能是也可能不是最好的地方——对于用户未明确从应用程序下载的东西,它不是我的首选。
关于android - 保存到下载文件夹的文件未显示在 "Downloads"应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48592356/