标签: android-download-manager

如何在Android中使用intentservice同时下载多个文件?

我想创建一个类似于这个的服务(从这里引用),以便在Android中异步下载多个文件.

public static class DownloadingService extends IntentService {
public static String PROGRESS_UPDATE_ACTION = DownloadingService.class
        .getName() + ".newDownloadTask";
private ExecutorService mExec;
private CompletionService<NoResultType> mEcs;
private LocalBroadcastManager mBroadcastManager;
private List<DownloadTask> mTasks;

public DownloadingService() {
    super("DownloadingService");
    mExec = Executors.newFixedThreadPool( 3 ); // The reason to use multiple thread is to download files asynchronously. 
    mEcs = new ExecutorCompletionService<NoResultType>(mExec);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return super.onStartCommand(intent, flags, startId);
}

@Override
protected void onHandleIntent(Intent intent) {

  while(true)
  { …
Run Code Online (Sandbox Code Playgroud)

java multithreading android android-download-manager android-intentservice

4
推荐指数
1
解决办法
1万
查看次数

Android安装apk而无需用户许可

我正在构建一个用于自助服务终端模式的应用程序.该设备没有root权限.该应用程序是设备管理员(android.permission.BIND_DEVICE_ADMIN).我希望该应用能够下载并直接安装其他apks到平板电脑,而无需征得许可.

我正在使用Android DownloadManager下载apk:然后:

Intent intent = new Intent(Intent.ACTION_VIEW);
                                intent.setDataAndType(Uri.fromFile(new File(filePath)), "application/vnd.android.package-archive");
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
                                DownloadManagerActivity.this.startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

要安装apk.但这会带来一个确认弹出窗口.是下载后直接安装apk的另一个吗?谢谢

更新:

我发现了这篇文章:https: //paulononaka.wordpress.com/2011/07/02/how-to-install-a-application-in-background-on-android/

但它不起作用,而且非常有限.

java android android-intent apk android-download-manager

4
推荐指数
1
解决办法
3847
查看次数

从内部存储的下载文件夹中打开下载的文件 (pdf)

我已经使用下载管理器下载了一个文件(133465.pdf),现在它存储在手机的下载文件夹中(内部存储)。

我应该如何尝试从下载文件夹中检索下载的 pdf?

我正在使用以下代码尝试从下载文件夹中检索 pdf,但在 Toast 上出现错误,提示“无法显示 PDF(133465.pdf 无法打开)”。

String file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() +  File.separator + "133465.pdf";
                Log.i("Fragmentadapter1", file);
                File videoFile2Play = new File(file);
                Intent i = new Intent();
                i.setAction(android.content.Intent.ACTION_VIEW);
                i.setDataAndType(Uri.fromFile(videoFile2Play), "application/pdf");
                imageContext.startActivity(i);
Run Code Online (Sandbox Code Playgroud)

我不知道我是否使用正确的文件位置来访问文件。

任何帮助或建议将不胜感激。

pdf android file android-intent android-download-manager

4
推荐指数
1
解决办法
8628
查看次数

如何检查文件是否在下载管理器队列中?

用户必须下载多个文件,这些文件将通过单击按钮开始。该代码将检查文件是否已存在,如果不存在,则将其添加到下载队列中。我正在将所有不存在的文件添加到下载队列中。

但是,如果用户立即再次单击该按钮,文件将重新添加到下载队列中。那么如何检查文件是否添加到下载管理器队列中呢?如果添加,则不会重新添加该文件。

android android-download-manager

4
推荐指数
1
解决办法
2179
查看次数

使用 DownloadManager 下载视频文件时,应用程序在 Android 10 上崩溃

设置时应用程序崩溃 setDestinationInExternalPublicDir()

 val videoDir = Environment.getDataDirectory().absolutePath + "/applicationfiles/.tutorial"
downloadRequest.setDestinationInExternalPublicDir(videoDir, videoName)
Run Code Online (Sandbox Code Playgroud)

E/AndroidRuntime: FATAL EXCEPTION: main Process: app.package, PID: 19275 java.lang.IllegalStateException: 不是标准目录之一: /data/applicationfiles/.tutorial at android.os.Parcel.createException(Parcel.java:2079 ) 在 android.os.Parcel.readException(Parcel.java:2039) 在 android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:188) 在 android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140) 在 android.content .ContentProviderProxy.call(ContentProviderNative.java:658) at android.content.ContentProviderClient.call(ContentProviderClient.java:558) at android.content.ContentProviderClient.call(ContentProviderClient.java:546) at android.app.DownloadManager$Request。 setDestinationInExternalPublicDir(DownloadManager.java:569) at app.package.startDownloadingFile(ExerciseTutorialPresenter.kt:159) at app.package.downloadVideoFiles(ExerciseTutorialPresenter.kt:142)

android download-manager android-download-manager

4
推荐指数
1
解决办法
2816
查看次数

安卓下载管理器“下载不成功”

这是我第一次尝试实施DownloadManager,无论我尝试什么,我总是收到“下载失败”的通知。我查看了许多其他 SO 论坛、一些教程以及我应该使用的内容。是的,我已经在清单文件中设置了 Internet 和外部存储权限。是的,我已经在手机的应用程序设置中授予了存储权限。我已经在运行 API 28 的 Android 模拟器和运行相同的真实手机上尝试过这个。这是我的代码:

        String url = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4";

        DownloadManager downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

        request.setTitle("title");

        request.setDescription("Your file is downloading");

        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_MUSIC, "" + System.currentTimeMillis());

        request.allowScanningByMediaScanner();
        request.setAllowedOverMetered(true);
        request.setAllowedOverRoaming(true);

        //Enqueue download and save the referenceId
        long downloadReference = downloadManager.enqueue(request);

        if (downloadReference != 0) {
            Toast.makeText(getApplicationContext(), "download started", Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(getApplicationContext(), "no download started", Toast.LENGTH_SHORT).show();
        }
Run Code Online (Sandbox Code Playgroud)

任何帮助或建议表示赞赏。谢谢。

android android-download-manager

4
推荐指数
1
解决办法
2317
查看次数

在应用程序中创建 CSV 或 TXT 文件并将其保存到“下载”文件夹 - Android

在问这个问题之前我进行了很多搜索和尝试。但我尝试的所有代码都不起作用。我希望该文件存储在该download文件夹中,并且在用户卸载该应用程序时也可以访问该文件。我也尝试过使用opencsv库。您能否提供一种tested创建csvtxt文件并将其存储到download文件夹的方法?

csv android opencsv android-download-manager android-storage

4
推荐指数
1
解决办法
4567
查看次数

Android下载队列使用DownloadManger

我使用的下载管理器下载我的文件在Android和它的伟大,因为它处理一切(连接丢失,重试等)问题是我希望我的文件在队列中的一个又一个被下载并据我所知下载管理没有按不提供此功能.因此,对DownloadManager.enqueue(...)的多次调用会导致所有文件的并发下载.我怎样才能解决这个问题?

我不能只在我的活动中排队并逐个发送下载到DownloadManger,因为活动可能随时被销毁!

另外IntentService在这里不起作用!! 即使它逐个处理请求,调用DownloadManager.enqueue()将运行得如此之快,然后下一次调用,结果将再次并发下载!

我的第三个选择是使用获取请求的LocalService并在先前开始的下载完成时调用DownloadManager.enqueue()但我应该怎么做?我的服务需要从我的活动中得到请求,即使它正在运行!(所以我不能把数据放在意图中).为了实现通信,我需要使它成为一个绑定服务,并且当文档表明它没有任何绑定时它会破坏!

bound service runs only as long as another application component is bound to it. 
Multiple components can bind to the service at once, but when all
of them unbind, the service is destroyed.
Run Code Online (Sandbox Code Playgroud)

因此,当我的活动关闭时,我会丢失队列中的下载.我对吗?

并且有最终选项在单独的过程中使用服务,因为即使我的第三个选项有效,只要应用程序未关闭,它就只下载文件.这个选项似乎是可怕的,因为我必须处理进程间通信,我不知道那是什么!

所以我错过了什么?!不应该是我的问题更容易解决?

我只是下载文件是队列!当无法下载时,我也不希望我的服务无限期运行.

android android-service android-download-manager android-service-binding

3
推荐指数
1
解决办法
8772
查看次数

在Android下载后打开下载的文件

我有一个应用程序,其中包含一些指向Web上文件的链接.我希望在用户选择将文件下载到设备后 - 该文件将自动打开.这是我的下载代码:

private void downloadFile(String url) {

        if (GeneralHelper.isNetworkAvailable(this)) {
            Uri uri = Uri.parse(url); 
            DownloadManager.Request r = new DownloadManager.Request(uri);

            String fileName = url.substring( url.lastIndexOf('/')+ 1, url.length() );

            // This put the download in the same Download dir the browser uses
            r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
            r.allowScanningByMediaScanner();

            // Notify user when download is completed
            // (Seems to be available since Honeycomb only)
            r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

            // Start download
            DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
            dm.enqueue(r);

        }
        else {
            // ....
        }

    }
Run Code Online (Sandbox Code Playgroud)

下载完成后,如何添加代码以打开文件?

android file android-download-manager

3
推荐指数
1
解决办法
6999
查看次数

Download Manger not working in Android Pie 9.0 NetworkSecurityConfig: No Network Security Config specified, using platform default

On updating the device OS to Android 9.0, previously this code was working fine(Xiaomi mi A2). Now, files are not being downloaded on Android Pie 9.0.

Moreover it is working fine in Oreo, Nougat , Marshmallow

Here is the code snippet:

File myDir = new File(Environment.getExternalStorageDirectory(), "MyApp");
        if (!myDir.exists()) {
            myDir.mkdirs();
        }
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                Locale.getDefault()).format(new Date());
        DownloadManager mgr = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        Uri downloadUri = Uri.parse(url);
        DownloadManager.Request request = new DownloadManager.Request(
                downloadUri);
        request.setAllowedNetworkTypes(
                DownloadManager.Request.NETWORK_WIFI …
Run Code Online (Sandbox Code Playgroud)

android download android-download-manager android-9.0-pie

3
推荐指数
1
解决办法
3965
查看次数