How to download part of a file from a url in android?

I am trying to download part of a file based on the download URL using setRequestProperty ("Range", "bytes =" + startbytes + "-" + endbytes); The following code snippet shows what I'm trying to do.

protected String doInBackground(String... aurl) { int count; Log.d(TAG,"Entered"); try { URL url = new URL(aurl[0]); HttpURLConnection connection =(HttpURLConnection) url.openConnection(); int lengthOfFile = connection.getContentLength(); Log.d(TAG,"Length of file: "+ lengthOfFile); connection.setRequestProperty("Range", "bytes=" + 0 + "-" + 1000); 

The problem is that an exception is raised that says: "Unable to set the request property after creating the connection." Please help me solve this problem.

+4
source share
2 answers

Assuming you are using HTTP for download, you will want to use the HTTP HEAD http and RANGE http headers.

HEAD will give you the file size (if available), and then RANGE allows you to load a range of bytes.

Once you have the file size, divide it into pieces of the same size and find the download stream for each fragment. Once done, write the file fragments in the correct order.

If you don't know how to use the RANGE header, here is another SO answer explaining how: fooobar.com/questions/398866 / ...

[EDIT]

To make the file in pieces, use this and start the download process,

 private void getBytesFromFile(File file) throws IOException { FileInputStream is = new FileInputStream(file); //videorecorder stores video to file java.nio.channels.FileChannel fc = is.getChannel(); java.nio.ByteBuffer bb = java.nio.ByteBuffer.allocate(10000); int chunkCount = 0; byte[] bytes; while(fc.read(bb) >= 0){ bb.flip(); //save the part of the file into a chunk bytes = bb.array(); storeByteArrayToFile(bytes, mRecordingFile + "." + chunkCount);//mRecordingFile is the (String)path to file chunkCount++; bb.clear(); } } private void storeByteArrayToFile(byte[] bytesToSave, String path) throws IOException { FileOutputStream fOut = new FileOutputStream(path); try { fOut.write(bytesToSave); } catch (Exception ex) { Log.e("ERROR", ex.getMessage()); } finally { fOut.close(); } } 
+3
source

Option 1

If you do not need to know the length of the content:

[Beware, do not call connection.getContentLength() . If you call it, you will get an exception. If you need to call it, check the second option]

 URL url = new URL(aurl[0]); HttpURLConnection connection =(HttpURLConnection) url.openConnection(); connection.setRequestProperty("Range", "bytes=" + 0 + "-" + 1000); //Note that, response code will be 206 (Partial Content) instead of usual 200 (OK) if(connection.getResponseCode() == HttpURLConnection.HTTP_PARTIAL){ //Your code here to read response data } 

Option 2

If you need to know the length of the content:

 URL url = new URL(aurl[0]); //First make a HEAD call to get the content length HttpURLConnection connection =(HttpURLConnection) url.openConnection(); connection.setRequestMethod("HEAD"); if(connection.getResponseCode() == HttpURLConnection.HTTP_OK){ int lengthOfFile = connection.getContentLength(); Log.d("ERF","Length of file: "+ lengthOfFile); connection.disconnect(); //Now that we know the content lenght, make the GET call connection =(HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Range", "bytes=" + 0 + "-" + 1000); //Note that, response code will be 206 (Partial Content) instead of usual 200 (OK) if(connection.getResponseCode() == HttpURLConnection.HTTP_PARTIAL){ //Your code here to read response data } } 
+7
source

Source: https://habr.com/ru/post/1483113/


All Articles