java下载的文件不会出现在下载文件夹中(android q)

jdgnovmf  于 2021-07-08  发布在  Java
关注(0)|答案(2)|浏览(351)

我已经寻找了很长一段时间的解决办法,但仍然没有找到合适的。
我的问题是。。。当下载一个android低于q的文件时,绝对没有问题,文件会出现在下载文件夹中。但是当下载android>=q的文件时,该文件不会出现在下载文件夹中。
所以我知道自从android10安装了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);
                    }
                }
            }
        });
vx6bjr1n

vx6bjr1n1#

新线程(“文件下载”)
你的错误代码从这里开始。
不要使用等待一秒钟的线程,假设下载已经完成。
可能因为文件还没有下载,所以uri是空的。
相反,您应该启动一个broadcastreceiver,它会在文件下载时通知您。
更新:如果您尝试使用http://url下载,那么您必须添加到 <application 清单文件中的标记:

android:usesCleartextTraffic="true"
eoigrqb6

eoigrqb62#

根据android文档,应用程序不能再使用downloadmanager下载到downloads目录。
你可以尝试添加 android:requestLegacyExternalStorage="true" 到androidmanifest中的应用程序。

相关问题