ContentResolver openInputStream downloads the file in the background

Viewed 1130

So I'm trying to download a huge file. So naturally my first step is to open the stream

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

My problem is that this instead of working quickly and just opening the stream, it in fact downloads the thing in the background. How do I know? Well this command blocks for like 5 minutes and then when I read from the stream, it's instantaneous.

Could I make this action prevent from download in the background? I want to inform the user about the progress and not just wait.

Edit: to make clear, I'm using a content:// URI for the resource

2 Answers

This is a wrong method to read a file over the network, you need to use network-specific methods.

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();

For long-running HTTP downloads you can use DownloadManager and show download progress e.g. like in this question of Victor 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();
Related