0

when i run this URL and try to save t\in .mp4 format it gives FileNotFound exception. Same URL if i try from browser , stored as in .mp4 format http://vimeo.com/moogaloop/play/clip:7926539/5cd4f7989ee0cb5018c131260aa1fc8c/1309860388/

Here is my code:

String storage = Environment.getExternalStorageState();
    Log.i("System out", "" + storage);
    File rootsd = Environment.getExternalStorageDirectory();
    Log.d("ROOT PATH", "" + rootsd.getAbsolutePath());

    File mydir = new File(rootsd.toString() + "/unplugger");
    Log.i("DIR PATH", "" + mydir.getAbsolutePath());
    mydir.mkdir();

    URL u;
    try {
        u = new URL(
                "http://vimeo.com/moogaloop/play/clip:7926539/5cd4f7989ee0cb5018c131260aa1fc8c/1309860388/");

        HttpURLConnection c = (HttpURLConnection) u.openConnection();
        c.setRequestMethod("GET");
        c.setDoOutput(true);

        c.connect();

        FileOutputStream f = new FileOutputStream(new File(mydir + "/"
                + "myVideo.mp4"));

        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();
        c.disconnect();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.i("System out","malformed :"+e.getMessage());
        Log.i("System out","malformed :"+e.toString());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.i("System out","io: "+e.getMessage());
        Log.i("System out","io: "+e.toString());
    }

thanks in advance.

woliveirajr
  • 9,433
  • 1
  • 39
  • 49
Hiren Dabhi
  • 3,693
  • 5
  • 37
  • 59
  • 1
    You have asked 22 questions but did not accept any of answer. accept answers first – Sunil Kumar Sahoo Jul 05 '11 at 12:03
  • Possible duplicate of [How to download and save a file from Internet using Java?](https://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java) – Robin Green Nov 17 '18 at 06:43

1 Answers1

2

Change this piece:

new File(mydir + "/" + "myVideo.mp4")

To be this instead:

new File(mydir, "myVideo.mp4")
Neil Traft
  • 18,367
  • 15
  • 63
  • 70
  • actually the problem is to get the data from url (filenotfound exception at this line : InputStream in = c.getInputStream(); ) – Hiren Dabhi Jul 05 '11 at 11:59
  • I don't think HttpURLConnection handles redirects automatically. Since your link does not point directly to the file itself, you are probably getting a 3xx HTTP response. This would be a redirect and would not contain any content, so therefore would throw a `FileNotFound` if you try to get the content. I would recommend using Apache's HttpClient libraries, which are available on Android. Don't use HttpURLConnection. – Neil Traft Jul 05 '11 at 13:40