如何将文件下载到本地下载文件夹?

How do i download files to the local downloads folder?

我使用 WebView 启用了文件下载设置。我正在使用 DownloadManager 保存文件。但是这些文件不会出现在本地下载目录中。我下载的文件都保存在这里。

> file/storage/emulated/0/Android/data/com.myapp/files/x.mp3

我已经尝试了很多。但是不知何故,它没有下载到本地下载文件夹中。我该怎么办?

My Code

String string = String.valueOf((URLUtil.guessFileName(url, contentDisposition, mimeType)));

            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
            request.setMimeType(mimeType);
            String cookies = CookieManager.getInstance().getCookie(url);

            request.addRequestHeader("cookie", cookies);
            request.addRequestHeader("User-Agent", userAgent);
            request.setTitle("test17");
            request.setDescription("Downloading file...");
            request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType));
            request.allowScanningByMediaScanner();

            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.setDestinationInExternalFilesDir(getContext(), DIRECTORY_DOWNLOADS ,  string);
            DownloadManager dm = (DownloadManager)getActivity().getSystemService(DOWNLOAD_SERVICE);
            dm.enqueue(request);

根据documentation,有两种类型的外部存储

  • Public files: Files that should be freely available to other apps and to the user. When the user uninstalls your app, these files should remain available to the user. For example, photos captured by your app or other downloaded files should be saved as public files.
  • Private files: Files that rightfully belong to your app and will be deleted when the user uninstalls your app. Although these files are technically accessible by the user and other apps because they are on the external storage, they don't provide value to the user outside of your app.

在您的代码中,调用 DownloadManager.Request.setDestinationInExternalFilesDir() 等同于调用 Context.getExternalFilesDir() 将获得私有文件目录。

如果要将下载的文件保存到下载目录,请使用DownloadManager.Request.setDestinationInExternalPublicDir()

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "x.mp3");
// call allowScanningByMediaScanner() to allow media scanner to discover your file
request.allowScanningByMediaScanner();

对于 Android Q 和之前的 Android Q (<= P),您需要使用 ContentResolver 将文件从存储位置复制到下载文件夹,该文件将出现在下载文件夹中

            val file = File(filePath)
            val manager =
                (context.getSystemService(Activity.DOWNLOAD_SERVICE) as DownloadManager)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {                 
                val resolver = context.contentResolver
                val contentValues = ContentValues().apply {
                    put(MediaStore.Files.FileColumns.DISPLAY_NAME, file.name)
                    put(MediaStore.Files.FileColumns.MIME_TYPE, "application/pdf")
                    put(
                        MediaStore.Files.FileColumns.RELATIVE_PATH,
                        Environment.DIRECTORY_DOWNLOADS
                    )
                }

                val uri = resolver.insert(
                    MediaStore.Downloads.EXTERNAL_CONTENT_URI,
                    contentValues
                )

                val fos = resolver.openOutputStream(uri!!)
                val fin = FileInputStream(file)
                fin.copyTo(fos!!, 1024)
                fos.flush()
                fos.close()
                fin.close()
            } else {
                var destination =
                    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
                        .toString() + "/" + file.name

                val uri = Uri.parse("file://$destination")
                val fos = context.contentResolver.openOutputStream(uri!!)
                val fin = FileInputStream(file)
                fin.copyTo(fos!!, 1024)
                fos.flush()
                fos.close()
                fin.close()
            }

在打开系统 "Downlod" 查看

时显示带有 "downloaded" 文件名的通知
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val name = "Some Channel"
            val descriptionText = "Default channel"
            val importance = NotificationManager.IMPORTANCE_DEFAULT
            val channel = NotificationChannel(DEFAULT_CHANNEL, name, importance).apply {
                description = descriptionText
            }
            // Register the channel with the system
            val notificationManager: NotificationManager =
                context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
        }

            val intent = Intent()
            intent.action = DownloadManager.ACTION_VIEW_DOWNLOADS
            val pendingIntent: PendingIntent = PendingIntent.getActivity(context, 0, intent, 0)

            val builder = NotificationCompat.Builder(context, DEFAULT_CHANNEL)
                .setSmallIcon(R.drawable.save_icon)
                .setContentTitle("123file")
                .setContentText("File succesfully exported")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                // Set the intent that will fire when the user taps the notification
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)

            with(NotificationManagerCompat.from(context)) {
                // notificationId is a unique int for each notification that you must define
                notify(135, builder.build())
            }