ContentResolver openInputStream在后台下载文件

时间:2018-11-15 14:44:20

标签: java android fileinputstream android-contentresolver

所以我试图下载一个大文件。所以自然地,我的第一步就是打开流

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

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

我可以使此操作阻止在后台下载吗?我想通知用户有关进度,而不仅仅是等待。

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

details_total