【问题标题】:Downloaded file from Android App not visible in Downloads directory从 Android 应用下载的文件在“下载”目录中不可见
【发布时间】:2012-08-30 19:52:46
【问题描述】:

我正在开发一个应用程序,我正在从远程服务器下载 pdf 文件。到目前为止,我通过该应用程序成功地在手机上下载了 PDF 文件。我面临的问题是下载的文件在我的 Galaxy Nexus 的下载目录中不可见。当我使用文件管理器应用程序时,我可以在那里看到文件并且它打开得非常好。

我尝试在我的代码中使用以下选项,但它们似乎都不能解决我的问题(这两个选项都成功下载了文件并且它在文件管理器中可见):

outFile = new File(Environment.getExternalStorageDirectory() + "/" + fileName);

outFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fileName);

有人可以帮我提供一些线索吗?任何提示或线索都会有很大帮助。

【问题讨论】:

  • 尝试使用DownloadManager,不知道是否可以将下载的文件放入下载列表中
  • 看看这个方法 addCompletedDownload here developer.android.com/reference/android/app/…
  • 当您说下载目录时,您是指下载应用程序吗?你是说你可以通过文件管理器看到
  • @zapl:非常感谢。 DownloadManager 就像一个魅力。它就像我需要的那样工作。我很高兴:)

标签: android download


【解决方案1】:

我可以通过以下方式使用 DownloadManager 做到这一点:

dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    Request request = new Request(
            Uri.parse("http://"));
    enqueue = dm.enqueue(request);

     BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                    long downloadId = intent.getLongExtra(
                            DownloadManager.EXTRA_DOWNLOAD_ID, 0);
                    Query query = new Query();
                    query.setFilterById(enqueue);
                    Cursor c = dm.query(query);
                    if (c.moveToFirst()) {
                        int columnIndex = c
                                .getColumnIndex(DownloadManager.COLUMN_STATUS);
                        if (DownloadManager.STATUS_SUCCESSFUL == c
                                .getInt(columnIndex)) {
                            Toast.makeText(getApplicationContext(), "Download Complete!!!", Toast.LENGTH_LONG).show();

                        }
                    }
                }
            }
        };

        registerReceiver(receiver, new IntentFilter(
                DownloadManager.ACTION_DOWNLOAD_COMPLETE));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-19
    • 2011-01-01
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    • 1970-01-01
    • 2016-05-28
    相关资源
    最近更新 更多