gpt4 book ai didi

java - 从 URL 下载 pdf 文件并将其保存在 android (java) 中的特定文件夹中

转载 作者:行者123 更新时间:2023-12-05 04:47:24 39 4
gpt4 key购买 nike

我想从给定的 URL 下载 pdf 文件并将其保存在特定文件夹中。到目前为止,我可以下载所需的文件并存储在“下载”文件夹中,我也可以创建自定义目录“AldoFiles”,但我无法将文件存储在自定义目录中。

这是我的尝试:

 private void downloadPdf() {

try {
url = new URL(pdfURL);
} catch (MalformedURLException e) {
e.printStackTrace();
}

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
requestPermissions(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},RequestCode);
}

File direct = new File(Environment.getExternalStorageDirectory() + "/Download/AldoFiles");

if (!direct.exists()) {
File pdfDirectory = new File("/sdcard/Download/AldoFiles/");
pdfDirectory.mkdirs();
}

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url + ""));
request.setTitle(fileName);
request.setMimeType("application/pdf");
request.allowScanningByMediaScanner();
request.setAllowedOverMetered(true);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,fileName);
DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
downloadManager.enqueue(request);

}

最佳答案

感谢@blackapps 我解决了这个问题:

try {
url = new URL(bookURL);
} catch (MalformedURLException e) {
e.printStackTrace();
}

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url + ""));
request.setTitle(fileName);
request.setMimeType("application/pdf");
request.allowScanningByMediaScanner();
request.setAllowedOverMetered(true);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "AldoFiles/" + fileName);
DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
downloadManager.enqueue(request);

关于java - 从 URL 下载 pdf 文件并将其保存在 android (java) 中的特定文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68507130/

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