簡體   English   中英

Android:從“下載”文件夾中刪除下載的文件快捷方式

[英]Android: Remove Downloaded file shortcut from "Downloads" folder

在我的代碼中,我曾經下載 apk,使用它然后將其刪除。 但是刪除它后,Apk 僅從內部存儲(內部存儲/android/data//files/download)中刪除,但它仍然在我的文件/下載文件夾中。 如何以編程方式從我的文件/下載文件夾中刪除此快捷方式/視圖,或如何防止將其保存到此文件夾中?

下載代碼:

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);

刪除代碼:

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

您也可以嘗試這種方式 - 希望它會起作用。 由於DownloadManager可以下載此uri的文件,因此您可以使用該uri刪除。

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

根據文檔enqueue 將返回一個 long 值,表示下載文件夾中此文件的 id。 將此值存儲在變量中

long fileId = manager.enqueue(request);

然后使用此值將其從下載中刪除

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();
}

確保你有寫外部存儲權限

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM