,可以通过以下步骤实现:
File hiddenFolder = new File(Environment.getExternalStorageDirectory(), ".hidden_folder");
if (!hiddenFolder.exists()) {
hiddenFolder.mkdirs();
}
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(fileUrl));
request.setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS, ".hidden_folder");
DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
long downloadId = downloadManager.enqueue(request);
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (downloadId != -1) {
DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadId);
Cursor cursor = downloadManager.query(query);
if (cursor.moveToFirst()) {
int statusIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status = cursor.getInt(statusIndex);
if (status == DownloadManager.STATUS_SUCCESSFUL) {
int uriIndex = cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI);
String downloadedUriString = cursor.getString(uriIndex);
if (downloadedUriString != null) {
File downloadedFile = new File(Uri.parse(downloadedUriString).getPath());
File hiddenFolder = new File(Environment.getExternalStorageDirectory(), ".hidden_folder");
File destinationFile = new File(hiddenFolder, downloadedFile.getName());
downloadedFile.renameTo(destinationFile);
}
}
}
cursor.close();
}
}
};
registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
这样,使用下载管理器将文件保存到内部存储中的隐藏文件夹中的过程就完成了。
隐藏文件夹的优势在于可以保护用户的隐私和安全,防止其他应用或用户意外访问或删除这些文件。这种方式适用于需要将下载的文件保存在内部存储中,并且希望对文件进行保护的场景,例如保存用户的敏感数据、配置文件等。
腾讯云相关产品和产品介绍链接地址:
Techo Day
云+社区技术沙龙[第11期]
DB・洞见
云+社区技术沙龙[第14期]
云+社区技术沙龙[第17期]
T-Day
Elastic 中国开发者大会
云+未来峰会
腾讯云GAME-TECH游戏开发者技术沙龙
云+社区技术沙龙[第20期]
微搭低代码直播互动专栏
领取专属 10元无门槛券
手把手带您无忧上云