我正在尝试将文件下载到目录,该目录可能会根据用户选择的目录而有所不同。我将当前目录存储在文件对象中,现在尝试下载该文件。文件已下载,但未下载指定目录。那么,我该怎么做才能获取所选目录中的文件。
// Getting path to store the file
String path = root.getAbsolutePath();
path += curr.getName();
request.setDestinationInExternalPublicDir(path, new File(url).getName());
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
Run Code Online (Sandbox Code Playgroud)
传递给 File 构造函数的名为 url 的字符串包含一个 URL。只需使用它来检索文件的名称即可。
更新:我刚刚找到该文件。是在手机里,不是在sd卡里。它位于此文件夹中,storage\emulated\00。不知道为什么?另外,我得到的绝对路径是 storage\emulated\0。
我有注册到 android.intent.action.DOWNLOAD_COMPLETE 的接收器。
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<receiver
android:name="com.myapp.listener.DownloadListenerService"
android:exported="true"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
这是我的接收器 DownloadListenerService.java:
package com.myapp.listener;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class DownloadListenerService extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("MYAPP","Start DownloadListenerService");
//some code
}
}
Run Code Online (Sandbox Code Playgroud)
仅当下载完成时,我才会在 logcat 消息中看到“启动 DownloadListenerService”。但是,如果下载管理器中的下载状态不成功,我在 logcat 中看不到任何消息。我如何才能发现我的下载未下载?
java android broadcastreceiver android-intent android-download-manager
Android N 在下载管理器通知中有一个新的取消按钮。
我想在我的应用程序中执行一些代码,以在用户按下此按钮时停止进度条。如果有的话,调用哪个方法?
另请注意,意图过滤器操作 DownloadManager.ACTION_NOTIFICATION_CLICKED 仅在用户单击通知本身时触发,而不是在他/她单击“取消”按钮时触发。
if_downloadManager = new IntentFilter();
if_downloadManager.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
if_downloadManager.addAction(DownloadManager.ACTION_NOTIFICATION_CLICKED);
br_downloadManager = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
....
}
if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(action)) {
// This code is not executed when the user presses the Cancel Button in the Download Manager Notification
}
}
};
Run Code Online (Sandbox Code Playgroud)
提前致谢。
android cancel-button android-notifications android-download-manager
我正在创建一个进度条来显示 Android 下载管理器中下载 apk 的百分比,但DownloadManager.COLUMN_TOTAL_SIZE_BYTES总是返回-1,并且当下载完成时,它会返回文件大小。
String url = Config.GET_APK+ finalApk;
final DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.addRequestHeader("Authorization", token);
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, finalApk);
Log.d("PATH1", String.valueOf(Environment.DIRECTORY_DOWNLOADS + "/" + finalApk));
// get download service and enqueue file
final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
final long downId = manager.enqueue(request);
Log.d("DOWNLOADID", String.valueOf(downId));
download.show();
new Handler().postDelayed(new Runnable() …Run Code Online (Sandbox Code Playgroud) 这不适用于某些设备。在三星设备中,他们不允许使用下载管理器下载文件。我已经在清单中定义了权限并获得了运行时权限。
DownloadManager downloadManager = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setTitle("");
request.setDescription("");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationUri(Uri.fromFile(imageFile));
request.setMimeType("*/*");
downloadManager.enqueue(request);
Run Code Online (Sandbox Code Playgroud) **我使用 Flutter Downloader Package 完成下载一些文件后,我的应用程序自动关闭并断开与 android studio 的连接。任何人都可以帮助我找到解决方案。
final status = await Permission.storage.request();
if (status.isGranted) {
await downloadPDF();
}
downloadPDF() async {
final externalDir = await getExternalStorageDirectory();
taskId = await FlutterDownloader.enqueue(
url: pdfURL,
savedDir: externalDir.path,
fileName: "Flamingo Order Details",
showNotification: true,
openFileFromNotification: true,
);
}
Run Code Online (Sandbox Code Playgroud)
这是我的控制台错误:
final status = await Permission.storage.request();
if (status.isGranted) {
await downloadPDF();
}
downloadPDF() async {
final externalDir = await getExternalStorageDirectory();
taskId = await FlutterDownloader.enqueue(
url: pdfURL,
savedDir: externalDir.path,
fileName: "Flamingo Order Details",
showNotification: true, …Run Code Online (Sandbox Code Playgroud) download-manager android-download-manager flutter flutter-dependencies flutter-packages
我想从互联网上下载一些音频文件。我想使用downloadmanager下载这个文件,但我不知道如何将这些文件保存到android中的特定内部存储位置。InternalStorage/folder/filename.mp3(这样我就可以轻松访问它们)。
manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse("URL");
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
long reference = manager.enqueue(request);
Run Code Online (Sandbox Code Playgroud)
对于下载很有用。但我们无法指定这些文件的位置。
那么如何指定这些文件保存在内部存储的特定位置呢?还有,如何access使用delete这些文件。
请不要提出这个问题,因为我在阅读很多文章时感到困惑。
如果有其他更好的选择或任何建议请评论
android android-download-manager android-internal-storage android-storage
目前我正在尝试使用 DownloadManager 下载文件,但这不起作用,下载开始,但下载后下载文件夹内没有文件。
这就是我的代码:
private void downloadAddon() {
try{
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
// request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
request.setTitle("download");
request.setDescription("apk downloading");
// request.setAllowedOverRoaming(false);
request.setDestinationUri(Uri.fromFile(new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) , "mod.mcpack")));
DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
long downloadID = downloadManager.enqueue(request);
//Just for testing
if (downloadComplete(downloadID)) {
Toast.makeText(this, "Download Status: Completed", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "Download Status: Error", Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
//Not required, there is no error that crashes the app
Toast.makeText(this, "Error catched: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
private boolean downloadComplete(long downloadId){
DownloadManager …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用DownloadManager从我的服务器解析带有JSESSIONID的cookie来获取一个zip文件.我正在使用HTTPCLIENT lib获取此JSESSIONID所有进程登录并设置变量JSESSIONID以供稍后在我的DownloadManager请求中使用.
我的下载请求:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(htmlUrlDownload));
request.addRequestHeader("Cookie", "JSESSIONID=" + JSESSIONID);
request.addRequestHeader(Constants.USER_AGENT, Constants.TARGET_REQUEST_HEADER);
request.setDescription("Baixando " + metaDado.getType());
request.setTitle("Download");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
String nameFile = offlineUuid + ".zip";
fileName = nameFile;
filePath = Environment.getExternalStorageDirectory() + File.separator + Environment.DIRECTORY_DOWNLOADS
+ File.separator + fileName;
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, nameFile);
final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
final long downloadId = manager.enqueue(request);
Run Code Online (Sandbox Code Playgroud)
问题是下载永远不会启动,COLUMN_REASON会将代码返回给ERROR_UNHANDLED_HTTP_CODE
但是,如果我尝试使用Dropbox链接下载相同的文件而无需身份验证或使用httpclient,它的工作完美,我做错了什么?
如何获得更好的消息错误?
嗨,大家好,我正在尝试使用Dropbox网址下载文件,我从Android的“下载文件”中复制了代码,并在使用DownloadManager类的ProgressDialog中显示了进度。
public void downloadFromDropBoxUrl(View view) {
//verfying if the downloadmanager is available first.
if (isDownloadManagerAvailable(getApplication())) {
String url = "https://www.dropbox.com/s/m4z5u9qstxdtbc3/AllExams22.pdf?dl=0";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Some descrition");
request.setTitle("Some title");
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "my-map.pdf");
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
}
public static …Run Code Online (Sandbox Code Playgroud)