Upload a large file (approximately 2 MB) to the server

I am developing a mobile application for Android that has the function of uploading files to the server sequentially.

I wanted to write a communication code with a client server, while preserving the criteria.

Since the file size that will be downloaded from the mobile device will be about 2 MB, we need to send the file in several parts (pieces of data) to the server. Before sending the file to the server, I need to determine the signal level at runtime. for example: In a Wi-Fi environment, where the signal strength will be greater, the number of blocks will be less and the block size will be larger compared to using a 2G / 3G connection (using a SIM card), where there are no pieces, there will be more and the piece size will be less.

Any suggestions / sample code to implement the same would be useful to me. Also, is there any Android or third-party API that can fulfill this requirement.

Thanks in advance.

NE

+4
source share
1 answer

You can look at PhoneStateListener to determine the signal strength. Then just install chunkSize based on this.

I had to write an application in which I uploaded / uploaded large files (at the top 10-20 MB) using httppost. Downloading is easy because you can use the http "range" header. I could not figure out how to do this at boot.

The best way I could come up with is to open the RandomAccessFile file, read one piece from your file and write it to a temporary file, and then load this temp file. Scroll through the entire file, creating a temporary file for each fragment. Keep track of which chunkNum you are in preference. If that succeeds, you set chunkNum back to 0.

If the connection is not made in the middle of the download, the next time you try to download it, first check your chunkNum priority to see if it is greater than 0. RandomAccessFile seek () method to move the file pointer to the corresponding fragment and resume.

Unfortunately, this method adds delay to creating temporary files, but I could not come up with a better solution.

+1
source

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


All Articles