简体   繁体   中英

Android: Remove Downloaded file shortcut from "Downloads" folder

In my code I used to download apk, use it and then remove it. But after removing it, Apk is removed only from internal storage (internal storage/android/data//files/download) but it still is in My Files/Downloads folder. How can I remove this shortcut/view from My Files/Downloads folder programmatically, or how to prevent saving it into this folder?

Downloading code:

final String destination = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + FORWARD_SLASH + TMP_APK_NAME;
final Uri uri = Uri.parse("file://" + destination);

//set downloadmanager
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

//set destination
request.setDestinationUri(uri);

// get download service and enqueue file
final DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);

Deletion code:

//Delete update file if exists
final File file = new File(destination);
if (file.exists()) {
    file.delete();
}

You may try with this way also - Hope it will work. As the DownloadManager can download the file in this uri so you may delete with that uri .

// Delete update file if exists
File file = new File(uri.getPath());
if (file.exists()) {
    file.delete();
}

According to the documentation enqueue will return a long value which represents the id of this file in the download folder. Store this value in a variable

long fileId = manager.enqueue(request);

then use this value to remove it from downdloads

manager.remove(fileId);
final String destination = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + FORWARD_SLASH + TMP_APK_NAME;
final Uri uri = Uri.parse("file://" + destination);

File file = new File(uri.getPath());
if (file.exists()) { // if your file exit 
  file.delete();
}

make sure you have write external storage permission

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM