【问题标题】:ContentResolver openInputStream downloads the file in the backgroundContentResolver openInputStream 在后台下载文件
【发布时间】:2018-11-15 14:44:19
【问题描述】:

所以我正在尝试下载一个大文件。所以很自然我的第一步就是打开流

RNFetchBlob.RCTContext.getContentResolver().openInputStream(Uri.parse(fromUri));

我的问题是,它实际上不是快速工作并只是打开流,而是在后台下载东西。我怎么知道?好吧,这个命令会阻塞 5 分钟,然后当我从流中读取时,它是瞬时的。

我可以让这个动作阻止在后台下载吗?我想通知用户进度而不是等待。

编辑:为了清楚起见,我使用 content:// URI 作为资源

【问题讨论】:

  • 找到任何解决方案了吗?从 GDrive 导入 116MB 文件时我遇到了同样的问题 - 我的应用程序在 openInputStream 上“停止”。我可能会为此启动一个线程,但谁知道该调用是否会结束。

标签: java android fileinputstream android-contentresolver


【解决方案1】:

这是通过网络读取文件的错误方法,您需要使用特定于网络的方法。

HttpURLConnection request = 
    (HttpURLConnection)(new URL(Uri.parse(fromUri)).openConnection());
// Connect to the server, expect a delay and draw a progress bar
request.connect();
// This will read HTTP headers with content length, expect another delay
// Must be called before request.getContentLength()
request.getInputStream();
// Get the size of your file, to draw your progress bar properly
long total = request.getContentLength();
// Now you can finally start reading the data
InputStream input = request.getInputStream();

【讨论】:

  • 谢谢,去试试!一件小事 - 我的 URI 是 content:// 的东西,希望它也适用:)
  • 那会是个问题,HttpURLConnection 只处理 http://https:// URI。 content:// URI 无法做到这一点,它是由内容提供商代码下载的,而不是由您的应用代码下载的,并且应用无法控制下载进度。
  • 所以没有办法获得下载进度,而且android社区中没有人抱怨过吗? (我用谷歌搜索了很多)
【解决方案2】:

对于长时间运行的 HTTP 下载,您可以使用 DownloadManager 并显示下载进度,例如比如thisVictor Laerte的问题:

    String urlDownload = <YOUR_URL>;
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(urlDownload));

    request.setDescription("Testando");
    request.setTitle("Download");
    request.allowScanningByMediaScanner();
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,"teste.zip");

    final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);

    final long downloadId = manager.enqueue(request);

    final ProgressBar mProgressBar = (ProgressBar) findViewById(R.id.progressBar1);

    new Thread(new Runnable() {

        @Override
        public void run() {

            boolean downloading = true;

            while (downloading) {

                DownloadManager.Query q = new DownloadManager.Query();
                q.setFilterById(downloadId);

                Cursor cursor = manager.query(q);
                cursor.moveToFirst();
                int bytes_downloaded = cursor.getInt(cursor
                        .getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));

                if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
                    downloading = false;
                }

                final int dl_progress = (int) ((bytes_downloaded * 100l) / bytes_total);

                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {

                        mProgressBar.setProgress((int) dl_progress);

                    }
                });

                Log.d(Constants.MAIN_VIEW_ACTIVITY, statusMessage(cursor));
                cursor.close();
            }

        }
    }).start();

【讨论】:

  • 您好,请确认一下,这适用于 content:// 地址吗?
  • 似乎DownloadManager content:// frendly: 重要的是DownloadManager返回的Uri实际上是一个content://...对其他应用非常友好的Uri andf允许他们通过DownloadManager提供的ContentProvider轻松访问内容
  • @Novellizator 还请查看thisthat 问题和答案。
  • 所以最后看起来并不那么容易。尝试了你的方法,得到:java.lang.IllegalArgumentException: Can only download HTTP/HTTPS URIs: content://com.google.android.apps.docs.storage/document/acc%3D2%3Bdoc%3D43
  • @Novellizator 你的fromUri 是什么类型的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-07
  • 1970-01-01
  • 2016-07-17
  • 2020-03-10
相关资源
最近更新 更多