我使用DownloadManager,从Android 2.3开始,可以下载FTP.但它不起作用.
我不知道如何使它工作?我找不到合适的例子.是否DownloadManager支持FTP?
我正在使用我的服务器下载图像Download Manager.
它下载文件并将其放在我想要的位置.但由于某种原因通知坚持,我似乎无法删除它.下载管理器的代码如下:
mDownloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
Uri uri = Uri.parse("URL"));
long enqueue = mDownloadManager.enqueue(new DownloadManager.Request(uri)
.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI)
.setAllowedOverRoaming(false)
.setTitle("Title")
.setDescription("File description")
.setDestinationInExternalPublicDir("Folder", "Filename")
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE));
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
Toast.makeText(getApplicationContext(), "Download Completed", Toast.LENGTH_SHORT).show();
}
};
Run Code Online (Sandbox Code Playgroud)
下载后如何删除通知?.
我已经尝试设置所有不同的通知可见性模式,没有运气.一旦完成,我可以从BroadcastReceiver做些什么吗?
我正在构建一个应用程序,出于所有并发原因,该应用程序需要了解所有下载是否均已完成。只有当我所有的下载完成后,才能启动某个功能。
我设法编写了一个检查队列中旧下载的函数:
DownloadManager dm = (DownloadManager) context.getSystemService(context.DOWNLOAD_SERVICE);
Query q = new Query();
q.setFilterByStatus(DownloadManager.STATUS_FAILED|DownloadManager.STATUS_PENDING|DownloadManager.STATUS_RUNNING);
Cursor c = dm.query(q);
Run Code Online (Sandbox Code Playgroud)
问题是-可以肯定的是-在初始化时,我想清理队列并删除所有条目。
有什么想法我现在可以删除条目吗?
此功能对我不起作用,因为我不想物理删除文件...只需清空队列。
有任何想法吗?
我正在下载一个zip文件夹并保存在我的Android设备中的特定文件夹中.我的应用程序没有访问该文件夹,因为它是压缩的.我想从服务器下载后解压缩文件夹并保存在特定文件夹中.
我的代码在这里:
public void DownloadDatabase(String DownloadUrl, String fileName) {
try {
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File(root.getAbsolutePath() + "/timy/databases");
if(dir.exists() == false){
dir.mkdirs();
}
URL url = new URL("http://myexample.com/android/timy.zip");
File file = new File(dir,fileName);
long startTime = System.currentTimeMillis();
Log.d("DownloadManager" , "download url:" +url);
Log.d("DownloadManager" , "download file name:" + fileName);
URLConnection uconn = url.openConnection();
uconn.setConnectTimeout(TIMEOUT_SOCKET);
InputStream is = uconn.getInputStream();
ZipInputStream zipinstream = new ZipInputStream(new BufferedInputStream(is));
ZipEntry zipEntry;
while((zipEntry = zipinstream.getNextEntry()) != null){
String zipEntryName = zipEntry.getName();
File …Run Code Online (Sandbox Code Playgroud) 我必须使用DownloadManager存储从url下载的图像,并将其存储到sdcard中,并带有我自己的目录,例如“ xyz”。这是我的代码
File img_directory = null;
img_directory = new File(Environment.getExternalStorageDirectory() + "/xyz");
if (!img_directory.exists()) {
img_directory.mkdirs();
DownloadManager mgr = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse("my image url");
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(true)
.setTitle("Demo")
.setDescription("Something useful. No, really.")
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
.setDestinationInExternalPublicDir(Environment.getExternalStorageDirectory().getAbsolutePath() + "/xyz", image.jpeg);
mgr.enqueue(request);
}
Run Code Online (Sandbox Code Playgroud)
此代码将在Android 5.1.1上运行。当我在6.0中运行相同的代码时,会引发如下错误
Caused by: java.lang.IllegalStateException: Unable to create directory: /storage/emulated/0/storage/emulated/0/xyz at android.app.DownloadManager$Request.setDestinationInExternalPublicDir(DownloadManager.java:538)
Run Code Online (Sandbox Code Playgroud)
我已在清单文件中添加了“读取”和“写入”权限。如何解决此错误?有谁能帮助我?提前致谢。
android android-sdcard imagedownload android-download-manager
我正在尝试使用以下代码从URL下载图像:-
public static void writeToDisk(Context context, @NonNull String imageUrl, @NonNull String downloadSubfolder) {
Uri imageUri = Uri.parse(imageUrl);
String fileName = imageUri.getPath();
String downloadSubpath = downloadSubfolder + fileName;
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(imageUri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDescription(imageUrl);
request.allowScanningByMediaScanner();
request.setDestinationUri(getDownloadDestination(downloadSubpath));
downloadManager.enqueue(request);
}
Run Code Online (Sandbox Code Playgroud)
下载开始后,我不知道如何取消下载。
我正在从webview下载文件.但是, 在使用DownloadManager时,它不会显示与此类似的正在进行的下载通知.它只是在后台操作.下载过程中如何显示状态栏通知.我能够获取下载的文件但是如何在通知中显示正在进行的过程?
我使用了"request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);" 但它没有显示正在进行的过程.请指导我做错了什么.
这是我用过的代码.
mWebview.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
request.setMimeType("pdf");
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
request.addRequestHeader("User-Agent", userAgent);
request.setDescription("Downloading file...");
request.setTitle(URLUtil.guessFileName(url, contentDisposition,
"pdf"));
request.allowScanningByMediaScanner();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
//clueless why it's not working..
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
} else {
request.setShowRunningNotification(true);
}
//request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(
url, contentDisposition, "pdf"));
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
downloadReference = dm.enqueue(request);
IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE); …Run Code Online (Sandbox Code Playgroud) DownloadManager在Android 8.0上不起作用.我不知道为什么.有人能帮助我吗?
这是我尝试过的:
val downloadBroadcastReceiver = DownloadBroadcastReceiver()
context.registerReceiver(downloadBroadcastReceiver, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE))
request = DownloadManager.Request(Uri.parse(url))
val mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(folder + File.separator + fileName))
request.setMimeType(mimeType)
request.setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS, fileName)
request.setTitle(title)
request.setDescription(description)
request.setNotificationVisibility(VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
request.allowScanningByMediaScanner()
id = downloadManager.enqueue(request)
Run Code Online (Sandbox Code Playgroud) 我正在使用android.app.DownloadManager在android电视上下载大文件-(约700mb)-7.1.1。因此,当服务器以1mb / s的速度为我提供文件时,一切都很好。但是,当速度为500kb / s时,我无法下载文件。几乎一半的文件都以运行状态加载,之后DownloadManager给出状态为FAILED,原因为ERROR_CANNOT_RESUME。
我正在查看源代码,发现android.provider.Downloads具有STATUS_CANNOT_RESUME,该错误来自489错误代码。
并没有太多有关确切发生了什么的信息。因此,如果您遇到相同的问题,请给我一些建议。
再过1次-如果速度较快,则应用运行良好。
我正在研究一个Android应用,该应用使用WiFi连接到本地Flask服务器。然后,该应用程序显示存储在服务器(RPi3)上的图像。单击图像会触发下载请求,我希望Android DownloadManager将请求加入队列并下载图像。 WiFi网络不提供互联网访问。
到目前为止,我已经能够在Android 6和Android 8.1设备上对其进行测试,并且一切正常。在多个Android 9设备上进行测试后,下载未开始,但是从本地网络断开连接后,将显示失败的尝试。
阅读有关此内容的其他主题,我尝试了以下方法:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
Run Code Online (Sandbox Code Playgroud)
并添加在AndroidManifest.xml中:
android:networkSecurityConfig="@xml/network_security_config"
Run Code Online (Sandbox Code Playgroud)
如此处第一个答案所述,绑定网络:在 没有Internet连接的情况下使用WiFi
使用DownloadManager.Request类提供的选项,但它们似乎与此问题无关:
.setRequiresCharging(false)
.setAllowedOverMetered(false)
.setAllowedOverRoaming(false)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, (String) url);
Run Code Online (Sandbox Code Playgroud)
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
Run Code Online (Sandbox Code Playgroud)
这是我创建的下载请求:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url))
.setTitle("Some Title") …Run Code Online (Sandbox Code Playgroud)