问题
I enabled the download settings for files with WebView. I'm saving files with DownloadManager. But the files do not appear in the local downloads directory. The files I've downloaded are save here.
> file/storage/emulated/0/Android/data/com.myapp/files/x.mp3
I've tried a lot. But somehow it was not downloaded in the local downloads folder. What should I do?
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);
回答1:
According to documentation there are 2 types of external storage
- 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.
In your code, calling DownloadManager.Request.setDestinationInExternalFilesDir() is equivalent to calling Context.getExternalFilesDir()
which will get private file directory.
If you want to save downloaded files to Download directory, use DownloadManager.Request.setDestinationInExternalPublicDir()
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "x.mp3");
// call allowScanningByMediaScanner() to allow media scanner to discover your file
request.allowScanningByMediaScanner();
回答2:
For Android Q and prior Android Q (<= P) you need to copy file from your storage location to Download folder using ContentResolver, the file will appear in Download folder
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()
}
After just show notification with "downloaded" file name that opens system "Downlod" view
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())
}
来源:https://stackoverflow.com/questions/52359581/how-do-i-download-files-to-the-local-downloads-folder