[英]how to download and save media files in internal storage android java
我想从互联网上下载一些音频文件。 我想使用downloadmanager来下载这个文件,但我不知道如何将这些文件保存到android中的特定内部存储位置。 InternalStorage/folder/filename.mp3
(这样我可以轻松访问它们)。
manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse("URL");
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
long reference = manager.enqueue(request);
它对下载很有用。 但是我们无法指定这些文件的位置。
那么如何指定这些文件保存在内部存储的特定位置。 还有,如何access
和delete
这些文件。
请不要提出这个问题,因为我在阅读许多文章时感到困惑。
如果有任何其他更好的选择或任何建议评论下来
在此处检查 request.setDestination 函数 将文件存储在外部应用程序特定目录 [例如:“ external/Android/data/your_app_name/filePath_you_set_in_function ”],使用如下:
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setDescription("Selected Video is being downloaded");
request.allowScanningByMediaScanner();
request.setTitle("Downloading Video");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
//Set the local destination for the downloaded file to a path within the application's external files directory
request.setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS, fileName); //To Store file in External Public Directory use "setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName)"
DownloadManager manager = (DownloadManager)
mContext.getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
如果你想将它下载到另一个地方,你需要在下载完成后移动它,使用 IO 流。 下载管理器完成下载后,您可以使用广播接收器执行此任务。 您可以使用 FileProvider 在 Android 版本 10 或更高版本中使用其他应用程序打开文件。
只需添加这一行来保存文件:
request.setDestinationInExternalFilesDir(context,Environment.DIRECTORY_ALARM,mp3name);
删除此文件
File file = new
File(context.getExternalFilesDir(Environment.DIRECTORY_ALARMS),"Fav_Ringtone.mp3");
file.delete();
读取这个文件
File file= newFile(context.getExternalFilesDir(Environment.DIRECTORY_ALARMS),"Fav_Ringtone.mp3");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.