尝试从android上的url下载文件时出现filenotfoundException

时间:2014-07-15 05:42:15

标签: android download filenotfoundexception

我有这个网址http://app.oviewz.com/proceso_seleccions/110.png?style=medium&token=q9W0BQpU。当你把任何网页浏览器自动下载文件 但是当我尝试使用这个Android代码下载时:

                String urllogo="http://app.oviewz.com/proceso_seleccions/110.png?style=medium&token=q9W0BQpU";
                URL url = new URL(urllogo);
                HttpURLConnection urlConnection = (HttpURLConnection) url
                        .openConnection();

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

                // <span id="IL_AD2" class="IL_AD">connect</span>
                urlConnection.connect();

                // set the path where we want to save the file
                File SDCardRoot = Environment.getExternalStorageDirectory();
                // create a new file, to save the <span id="IL_AD7"
                // class="IL_AD">downloaded</span> file
                File file = new File(SDCardRoot, "logo.jpg");

                FileOutputStream fileOutput = new FileOutputStream(file);

                // Stream used for reading the data from the internet
                InputStream inputStream = urlConnection.getInputStream();

                // this is the total size of the file which we are <span
                // id="IL_AD12" class="IL_AD">downloading</span>

                // create a buffer...
                byte[] buffer = new byte[1024];
                int bufferLength = 0;

                while ((bufferLength = inputStream.read(buffer)) > 0) {
                    fileOutput.write(buffer, 0, bufferLength);

                }
                // close the output stream when complete //
                fileOutput.close();

引发FileNotFoundException。任何人都可以帮助我,我做错了什么?

1 个答案:

答案 0 :(得分:1)

当我在浏览器中打开此网址时,会将我重定向到http://app.oviewz.com/。这意味着图像无法公开显示,即您需要登录才能查看图像。因此,您无法使用网址直接下载图片。如果网站有一些用于登录的API,您可以使用它来获取所需的访问令牌,并在此请求的标题中发送相同的内容,就像他们在facebook或g + API中所做的那样。