java - Android 版本 > 2.3 上的 FileNotFoundException

标签 java android inputstream filenotfoundexception

我尝试将文件下载到手机的 SD 卡中。在 Android 2.1、2.2 和 2.3 上一切正常,但在 Android 4.0 上(在模拟器和我的 Galaxy Nexus 上测试)它抛出一个 FileNotFoundException

堆栈跟踪:

02-27 21:49:06.733: W/System.err(27648): java.io.FileNotFoundException: http://www.wdr.de/wdrlive/media/wdr5.m3u
02-27 21:49:06.733: W/System.err(27648):    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
02-27 21:49:06.733: W/System.err(27648):    at de.arvidg.test.StartActivity$9.run(StartActivity.java:833)
02-27 21:49:06.733: W/System.err(27648):    at java.lang.Thread.run(Thread.java:856)


我下载文件的方法:

downloadFile("http://www.wdr.de/wdrlive/media/wdr5.m3u");

    public void downloadFile(final String fileUrl) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {

                    URL url = new URL(fileUrl);

                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

                    urlConnection.setRequestMethod("GET");
                    urlConnection.setDoOutput(true);

                    urlConnection.connect();

//                  File cacheDir = appContext.getExternalCacheDir();
//                  File file = new File("/mnt/sdcard", "/cacheFile.ext");
                    File SDCardRoot = Environment.getExternalStorageDirectory();
                    File file = new File(SDCardRoot,"/cacheFile.ext");

                    if(!file.exists()) file.createNewFile();

                    FileOutputStream fileOutput = new FileOutputStream(file);

                    InputStream inputStream = urlConnection.getInputStream();

                    int totalSize = urlConnection.getContentLength();
                    int downloadedSize = 0;

                    byte[] buffer = new byte[1024];
                    int bufferLength = 0;

                    while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
                        fileOutput.write(buffer, 0, bufferLength);
                        downloadedSize += bufferLength;
                        Log.d(TAG, "downloadedSize = " + downloadedSize + " | totalSize = " + totalSize);
//                      updateProgress(downloadedSize, totalSize);

                    }
                    fileOutput.close();

            } catch (MalformedURLException e) {
                    e.printStackTrace();
            } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }).start();
    }

最佳答案

简单删除 urlConnection.setDoOutput(true);

感谢 pfn @ #android-dev

关于java - Android 版本 > 2.3 上的 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9472425/

相关文章:

java - HttpUrlConnection 读取输入流 - 不同的输出结果

java - 从 HTTP 请求中获取完整路径

Java Swing GridBagLayout - 添加没有空格的按钮

java - 在 Servlet 上创建 ArrayList

java - 对输入流和从文件中读取感到困惑

java - Eclipse 不会读取配置文件

java - 启用 RawMessageDelivery 时的 Amazon SNS 通知验证

android - 从Android Studio中的素材资源文件夹加载html页面时出错

具有图像更改的Android小部件

android - 如果父级超过 650dp,则让 Linearlayout 宽度为 650dp,否则匹配父级