从android中的url保存文件

时间:2011-07-05 11:33:59

标签: android video vimeo

当我运行此URL并尝试以.mp4格式保存t \时,它会给出FileNotFound异常。 如果我从浏览器尝试相同的URL,以.mp4格式存储 http://vimeo.com/moogaloop/play/clip:7926539/5cd4f7989ee0cb5018c131260aa1fc8c/1309860388/

这是我的代码:

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

提前感谢。

1 个答案:

答案 0 :(得分:2)

更改此内容:

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

改为:

new File(mydir, "myVideo.mp4")