gpt4 book ai didi

android - 在 DownloadManager 中取消下载

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:58:18 26 4
gpt4 key购买 nike

我正在尝试使用以下代码从 URL 下载图像:-

public static void writeToDisk(Context context, @NonNull String imageUrl, @NonNull String downloadSubfolder) {
Uri imageUri = Uri.parse(imageUrl);
String fileName = imageUri.getPath();
String downloadSubpath = downloadSubfolder + fileName;

DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(imageUri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDescription(imageUrl);
request.allowScanningByMediaScanner();
request.setDestinationUri(getDownloadDestination(downloadSubpath));

downloadManager.enqueue(request);
}

下载开始后我不知道如何取消。

最佳答案

使用enqueue方法获取ID

long downloadID = downloadManager.enqueue(request);

然后,使用 remove 方法将 downloadID 传递给它。

downloadManager.remove(downloadID);

关于android - 在 DownloadManager 中取消下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39271027/

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