标签: android-download-manager

android2.3 DownloadManager

我使用DownloadManager,从Android 2.3开始,可以下载FTP.但它不起作用.

我不知道如何使它工作?我找不到合适的例子.是否DownloadManager支持FTP?

android download android-download-manager

5
推荐指数
1
解决办法
1348
查看次数

Android:DownloadManager - Notification Sticking

我正在使用我的服务器下载图像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做些什么吗?

android android-download-manager

5
推荐指数
1
解决办法
4411
查看次数

Android-DownloadManager-清除队列中的旧下载

我正在构建一个应用程序,出于所有并发原因,该应用程序需要了解所有下载是否均已完成。只有当我所有的下载完成后,才能启动某个功能。

我设法编写了一个检查队列中旧下载的函数:

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)

问题是-可以肯定的是-在初始化时,我想清理队列并删除所有条目。

有什么想法我现在可以删除条目吗?

此功能对我不起作用,因为我不想物理删除文件...只需清空队列。

有任何想法吗?

java android task-queue android-download-manager

5
推荐指数
1
解决办法
3107
查看次数

在Android中以编程方式解压缩文件

我正在下载一个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)

java android android-emulator android-download-manager

5
推荐指数
2
解决办法
2万
查看次数

原因:java.lang.IllegalStateException:无法在Android 6.0设备中创建目录

我必须使用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

5
推荐指数
1
解决办法
6665
查看次数

在DownloadManager中取消下载

我正在尝试使用以下代码从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)

下载开始后,我不知道如何取消下载。

android android-download-manager

5
推荐指数
1
解决办法
2228
查看次数

在进度搜索栏通知未显示在DownloadManager android中

我正在从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)

android android-notifications android-download-manager

5
推荐指数
1
解决办法
715
查看次数

DownloadManager无法在Android 8.0上运行

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 android-download-manager

5
推荐指数
1
解决办法
2472
查看次数

DownloadManager ERROR_CANNOT_RESUME,状态失败

我正在使用android.app.DownloadManager在android电视上下载大文件-(约700mb)-7.1.1。因此,当服务器以1mb / s的速度为我提供文件时,一切都很好。但是,当速度为500kb / s时,我无法下载文件。几乎一半的文件都以运行状态加载,之后DownloadManager给出状态为FAILED,原因为ERROR_CANNOT_RESUME。

我正在查看源代码,发现android.provider.Downloads具有STATUS_CANNOT_RESUME,该错误来自489错误代码。

android.provider.Downloads

并没有太多有关确切发生了什么的信息。因此,如果您遇到相同的问题,请给我一些建议。

再过1次-如果速度较快,则应用运行良好。

android android-download-manager

5
推荐指数
1
解决办法
409
查看次数

当从没有互联网的WLAN请求下载时,下载无法在Android 9上开始

我正在研究一个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)
.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)

android wifi flask android-download-manager android-9.0-pie

5
推荐指数
1
解决办法
440
查看次数