0

我从服务器获取一个 url,我想在我的 android htc 设备中保存视频。我收到一些错误:----- java.io.FileNotFoundException: /mnt/sdcard/543b8417-6e67-4755-8c68-b93373f9d9e2.wmv (Permission denied) & address family not supported。我已经在清单文件中给予了所有许可。我的代码是:- videoUrl = "http://125.63.71.222/fliptest/uservideo/543b8417-6e67-4755-8c68-b93373f9d9e2.wmv";

    String LocalFilePath = videoUrl.substring(
            videoUrl.lastIndexOf("/") + 1, videoUrl.length());
    File dire = new File(android.os.Environment.getExternalStorageDirectory(), LocalFilePath);






        int bytesread = 0;
        byte[] bytes = new byte[8192];
        InputStream strm = null;
        HttpResponse response = null;
        HttpEntity entity = null;
        String LocalFile = null;
        FileOutputStream foStream;
        BufferedOutputStream writer = null;
        try {
            DefaultHttpClient httpclient = new DefaultHttpClient();
            videoUrl = videoUrl.replace(" ", "%20");
            HttpGet httpget = new HttpGet(videoUrl);

            response = httpclient.execute(httpget);

            entity = response.getEntity();
            strm = entity.getContent();







            LocalFile = android.os.Environment.getExternalStorageDirectory() + LocalFilePath;
            //directory = null;
            foStream = new FileOutputStream(dire);
            writer = new BufferedOutputStream(foStream);
            do {
                bytesread = strm.read(bytes, 0, bytes.length);
                if (bytesread > 0) {
                    writer.write(bytes, 0, bytesread);
                    writer.flush();
                }
            } while (bytesread > 0);

            writer.close();
            foStream.close();

            strm.close();
            return;

        } catch (Exception e) {
        //  dire.delete();              
            e.printStackTrace();

        }
4

1 回答 1

0

我会稍微修改你的代码:

String videoUrl = "http://125.63.71.222/fliptest/uservideo/543b8417-6e67-4755-8c68-b93373f9d9e2.wmv";
String LocalFilePath = videoUrl.substring(videoUrl.lastIndexOf("/") + 1, videoUrl.length());
File dire = new File(android.os.Environment.getExternalStorageDirectory(), LocalFilePath);

int bytesread = -1; // i would change it to -1
byte[] bytes = new byte[8192];
InputStream strm = null;
FileOutputStream foStream;

try {
    videoUrl = videoUrl.replace(" ", "%20");
    URL url = new URL(videoUrl);
    URLConnection connection = url.openConnection();
    connection.connect();

    strm = new BufferedInputStream(url.openStream());
    foStream = new FileOutputStream(dire);

    //typical pattern for reading
    while ((bytesread = strm.read(bytes)) != -1) {
        foStream.write(bytes, 0, bytesread);
    }

    foStream.flush();
    foStream.close();
    strm.close();

} catch (Exception e) {
   ...
}

您需要两个权限才能运行此代码 - 您已经提到您拥有它们,但请快速提醒:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

并且不要忘记异步运行此任务。

此致

于 2012-09-29T14:21:26.653 回答