我在我的应用程序中生成一个excelsheet,生成时应该自动保存在任何Android设备的"Downloads"文件夹中,通常保存所有下载.
我有以下将文件保存在"我的文件"文件夹下 -
File file = new File(context.getExternalFilesDir(null), fileName);
Run Code Online (Sandbox Code Playgroud)
导致 -
W/FileUtils? Writing file/storage/emulated/0/Android/data/com.mobileapp/files/temp.xls
Run Code Online (Sandbox Code Playgroud)
我宁愿在生成Excel工作表时自动将生成的文件保存在"Downloads"文件夹中.
更新#1:请在此处查看快照.我想要的是用红色圈出的那个,如果有意义,你建议的内容将被存储在一个带圆圈的蓝色(/ storage/emulated/0/download)中.请告知我如何在一个带圆圈的红色文件中保存文件,即"下载"文件夹与/ storage/emulated/0 /下的"MyFiles"下的文件夹不同

Olu*_*mbi 31
使用它来获取目录:
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
Run Code Online (Sandbox Code Playgroud)
并且不要忘记在manifest.xml中设置此权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Run Code Online (Sandbox Code Playgroud)
Olu*_*mbi 13
现在从你的问题编辑我想更好地理解你想要的.下载菜单上显示的红色圆圈中显示的文件实际上是通过下载的文件下载的DownloadManager,虽然我给你的普遍步骤会将文件保存在下载文件夹中,但由于没有下载,因此它们不会显示在此菜单中.但是,要使其工作,您必须开始下载文件,以便在此处显示.
以下是如何开始下载的示例:
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "fileName");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); // to notify when download is complete
request.allowScanningByMediaScanner();// if you want to be available from media players
DownloadManager manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
manager.enqueue(request);
Run Code Online (Sandbox Code Playgroud)
此方法用于从Uri下载,我没有使用它与本地文件.
如果您的文件不是来自互联网,您可以尝试保存临时副本并获取该值的文件的Uri.
Mik*_*rin 12
根据文档,需要使用Storage Access Frameworkfor Other types of shareable content, including downloaded files.
将文件复制到外部存储示例:
// use system file picker Intent to select destination directory
private fun selectExternalStorageFolder(fileName: String) {
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = "*/*"
putExtra(Intent.EXTRA_TITLE, name)
}
startActivityForResult(intent, FILE_PICKER_REQUEST)
}
// receive Uri for selected directory
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
FILE_PICKER_REQUEST -> data?.data?.let { destinationUri ->
copyFileToExternalStorage(destinationUri)
}
}
}
// use ContentResolver to write file by Uri
private fun copyFileToExternalStorage(destination: Uri) {
val yourFile: File = ...
try {
val outputStream = contentResolver.openOutputStream(destination) ?: return
outputStream.write(yourFile.readBytes())
outputStream.close()
} catch (e: IOException) {
e.printStackTrace()
}
}
Run Code Online (Sandbox Code Playgroud)
Thi*_*now 10
只需使用DownloadManager下载生成的文件,如下所示:
File dir = new File("//sdcard//Download//");
File file = new File(dir, fileName);
DownloadManager downloadManager = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
downloadManager.addCompletedDownload(file.getName(), file.getName(), true, "text/plain",file.getAbsolutePath(),file.length(),true);
Run Code Online (Sandbox Code Playgroud)
这"text/plain"是您传递的mime类型,因此它将知道哪些应用程序可以运行下载的文件.这样做对我来说.
我使用以下代码使它在C#的Xamarin Droid项目中起作用:
// Suppose this is your local file
var file = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };
var fileName = "myFile.pdf";
// Determine where to save your file
var downloadDirectory = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads);
var filePath = Path.Combine(downloadDirectory, fileName);
// Create and save your file to the Android device
var streamWriter = File.Create(filePath);
streamWriter.Close();
File.WriteAllBytes(filePath, file);
// Notify the user about the completed "download"
var downloadManager = DownloadManager.FromContext(Android.App.Application.Context);
downloadManager.AddCompletedDownload(fileName, "myDescription", true, "application/pdf", filePath, File.ReadAllBytes(filePath).Length, true);
Run Code Online (Sandbox Code Playgroud)
现在,您的本地文件已“下载”到您的Android设备,用户会收到通知,并且对该文件的引用将添加到downloads文件夹中。但是,请确保在写入文件系统之前先征求用户的许可,否则将抛出“拒绝访问”异常。
| 归档时间: |
|
| 查看次数: |
39428 次 |
| 最近记录: |