gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 00:13:59 24 4
gpt4 key购买 nike

我使用 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);

最佳答案

对于 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()
}

在打开系统“下载” 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())
}

关于android - 如何将文件下载到本地下载文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52359581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com