Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android download binary file problems

I am having problems downloading a binary file (video) in my app from the internet. In Quicktime, If I download it directly it works fine but through my app somehow it get's messed up (even though they look exactly the same in a text editor). Here is a example:

    URL u = new URL("http://www.path.to/a.mp4?video");     HttpURLConnection c = (HttpURLConnection) u.openConnection();     c.setRequestMethod("GET");     c.setDoOutput(true);     c.connect();     FileOutputStream f = new FileOutputStream(new File(root,"Video.mp4"));       InputStream in = c.getInputStream();      byte[] buffer = new byte[1024];     int len1 = 0;     while ( (len1 = in.read(buffer)) > 0 ) {          f.write(buffer);     }     f.close(); 
like image 629
Isaac Waller Avatar asked Feb 23 '09 04:02

Isaac Waller


People also ask

Why does my phone keep downloading BIN files?

bin is typically associated with the Play Store I would begin there by clearing the cache for Play Store, Play Services, Instant Apps, and your system cache then reboot. If you have Instant Apps installed/enabled try deleting/disabling it to see if it stops.

Is binary file readable?

Binary files are not human readable and require a special program or hardware processor that knows how to read the data inside the file. Only then can the instructions encoded in the binary content be understood and properly processed.

How do you open a file for binary?

To open the Binary Editor on an existing file, go to menu File > Open > File, select the file you want to edit, then select the drop arrow next to the Open button, and choose Open With > Binary Editor.


2 Answers

I don't know if it's the only problem, but you've got a classic Java glitch in there: You're not counting on the fact that read() is always allowed to return fewer bytes than you ask for. Thus, your read could get less than 1024 bytes but your write always writes out exactly 1024 bytes possibly including bytes from the previous loop iteration.

Correct with:

 while ( (len1 = in.read(buffer)) > 0 ) {          f.write(buffer,0, len1);  } 

Perhaps the higher latency networking or smaller packet sizes of 3G on Android are exacerbating the effect?

like image 164
Ry4an Brase Avatar answered Sep 22 '22 12:09

Ry4an Brase


new DefaultHttpClient().execute(new HttpGet("http://www.path.to/a.mp4?video"))         .getEntity().writeTo(                 new FileOutputStream(new File(root,"Video.mp4"))); 
like image 39
njzk2 Avatar answered Sep 19 '22 12:09

njzk2