[英]Downloaded file does not appear in downloads folder (android Q)
我已經尋找了很長時間的解決方案,但我仍然沒有找到合適的解決方案。
我的問題是... 用Android低於Q下載文件時,絕對沒有問題,文件出現在下載文件夾中。 但是當下載Android >= Q 的文件時,該文件不會出現在下載文件夾中。
所以我知道自 Android 10 使用 DownloadManager 以來事情發生了變化。 我多次讀到您現在應該使用 StorageManager,我當時就是這樣做的。 所以我改變了這個:
if (download_uri == null) {
try {
URL rurl = new URL(url);
InputStream is = rurl.openStream();
DataInputStream dis = new DataInputStream(is);
byte[] buffer = new byte[4096];
File file = new File(getExternalFilesDir(null), nameOfFile);
FileOutputStream fos = new FileOutputStream(file);
int length;
while ((length = dis.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
manager.addCompletedDownload(nameOfFile, "File", true, mimetype, Environment.DIRECTORY_DOWNLOADS + "/" + nameOfFile, contentLength, true);
}
對此
if (download_uri == null) {
try {
URL rurl = new URL(url);
InputStream is = rurl.openStream();
DataInputStream dis = new DataInputStream(is);
byte[] buffer = new byte[4096];
File file = new File(getExternalFilesDir(null), nameOfFile);
FileOutputStream fos = new FileOutputStream(file);
int length;
while ((length = dis.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
final String relativeLocation = Environment.DIRECTORY_DOWNLOADS + "/" + nameOfFile;
ContentValues myContentValues = new ContentValues();
myContentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, nameOfFile);
myContentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, relativeLocation);
myContentValues.put(MediaStore.MediaColumns.MIME_TYPE, mimetype);
myContentValues.put(MediaStore.MediaColumns.IS_PENDING, 1);
} else {
manager.addCompletedDownload(nameOfFile, "FileDescription", true, mimetype, Environment.DIRECTORY_DOWNLOADS + "/" + nameOfFile, contentLength, true);
}
}
我多次看到此代碼,但在我的情況下,該文件尚未下載。 也許我忘記了一些導致問題的代碼?
我還看到有人使用的一行代碼:
StorageManager sm = (StorageManager)getSystemService(Context.STORAGE_SERVICE);
在我的代碼中,我仍然使用:
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
進而:
download_id = manager.enqueue(request);
Uri download_uri = manager.getUriForDownloadedFile(download_id);
我不知道如何使用 StorageManager(如果必須的話)......任何幫助表示贊賞。
這是我使用 DownloadManager 的舊代碼:
webView.setDownloadListener((url, userAgent, contentDisposition, mimetype, contentLength) -> {
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (checkPermission()) {
Log.d(LogTag, "Download URL:" + url);
Log.d(LogTag, "Download user-Agent:" + userAgent);
Log.d(LogTag, "Download contentDisposition:" + contentDisposition);
Log.d(LogTag, "Download MIME-Type:" + mimetype);
Log.d(LogTag, "Download length:" + ((Long) contentLength).toString());
final DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
String nameOfFile = URLUtil.guessFileName(url, null, mimetype);
request.setMimeType(mimetype);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setAllowedOverMetered(true);
request.setAllowedOverRoaming(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
request.setRequiresCharging(false);
request.setRequiresDeviceIdle(false);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, nameOfFile);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
request.allowScanningByMediaScanner();
}
request.setDescription("FileDescription");
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
request.addRequestHeader("User-Agent", userAgent);
request.setTitle(nameOfFile);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
//request.setVisibleInDownloadsUi(true);
final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
long download_id;
download_id = manager.enqueue(request);
}
}
}
});
根據Android 文檔,應用程序無法再使用 DownloadManager 下載到 Downloads 目錄。
您可以嘗試將android:requestLegacyExternalStorage="true"
到 AndroidManifest 中的應用程序。
new Thread("文件下載")
這里你的錯誤代碼開始了。
不要使用假設下載已經完成而等待一秒鍾的線程。
可能該 uri 為空,因為該文件尚未下載。
相反,您應該啟動一個廣播接收器,它會在文件下載時通知您。
更新:如果您嘗試使用 http:// url 下載,那么您必須添加到清單文件中的<application
標簽:
android:usesCleartextTraffic="true"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.