开发者

save file from url in android

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.printS开发者_Go百科tackTrace();
        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.


Change this piece:

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

To be this instead:

new File(mydir, "myVideo.mp4")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜