Skip to main content

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




Comments

  1. 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?

    ReplyDelete
  2. One problem is your reading of the buffer. If every read of the input stream is not an exact multiple of 1024 you will copy bad data. Use:

    byte[] buffer = new byte[1024];
    int len1 = 0;
    while ( (len1 = in.read(buffer)) != -1 ) {
    f.write(buffer,0, len1);
    }

    ReplyDelete
  3. new DefaultHttpClient().execute(new HttpGet("http://www.path.to/a.mp4?video"))
    .getEntity().writeTo(
    new FileOutputStream(new File(root,"Video.mp4")));

    ReplyDelete
  4. public class download extends Activity {

    private static String fileName = "sawan123.3gp";

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try {
    URL url = new URL("http://joomlavogue.in/staff/sawanmodi/sawan.3gp");
    HttpURLConnection c = (HttpURLConnection) url.openConnection();
    c.setRequestMethod("GET");
    c.setDoOutput(true);
    c.connect();

    String PATH = Environment.getExternalStorageDirectory()
    + "/download/";
    Log.v("log_tag", "PATH: " + PATH);
    File file = new File(PATH);
    file.mkdirs();
    File outputFile = new File(file, fileName);
    FileOutputStream fos = new FileOutputStream(outputFile);

    InputStream is = c.getInputStream();

    byte[] buffer = new byte[1024];
    int len1 = 0;
    while ((len1 = is.read(buffer)) != -1) {
    fos.write(buffer, 0, len1);
    }
    fos.close();
    is.close();
    } catch (IOException e) {
    Log.d("log_tag", "Error: " + e);
    }
    Log.v("log_tag", "Check: ");
    } }

    ReplyDelete
  5. Just use apache's copy method (Apache Commons IO) - the advantage of using Java!

    IOUtils.copy(is, os);


    Do not forget to close the streams in a finally block:

    try{
    ...
    } finally {
    IOUtils.closeQuietly(is);
    IOUtils.closeQuietly(os);
    }

    ReplyDelete
  6. I fixed the code based on previous feedbacks on this thread. I tested using eclipse and multiple large files. It is working fine. Just have to copy and paste this to your environment and change the http path and the location which you would like the file to be downloaded to.

    try {
    //this is the file you want to download from the remote server
    String path ="http://localhost:8080/somefile.zip";
    //this is the name of the local file you will create
    String targetFileName
    boolean eof = false;
    URL u = new URL(path);
    HttpURLConnection c = (HttpURLConnection) u.openConnection();
    c.setRequestMethod("GET");
    c.setDoOutput(true);
    c.connect();
    FileOutputStream f = new FileOutputStream(new File("c:\\junk\\"+targetFileName));
    InputStream in = c.getInputStream();
    byte[] buffer = new byte[1024];
    int len1 = 0;
    while ( (len1 = in.read(buffer)) > 0 ) {
    f.write(buffer,0, len1);
    }
    f.close();
    } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (ProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }


    Good luck
    Alireza Aghamohammadi

    ReplyDelete
  7. I like the new look of thehtml5 vedio player Player is faster is better and my slow computer with crappy video card will not got slow anymore, nice job you guys!

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex