文件未找到异常android fileoutput流

时间:2014-06-18 17:11:33

标签: java android file

我正在尝试从网址下载xml。 但我得到了java.io FileNotFound sdcard.cellofest.xml Permission Denied。 最后一行是生成错误。 我检查了谷歌,发现了很多修复,但没有解决我的问题。

我在AndroidManifest中拥有“写入外部存储”权限。

URL url = new URL("http://test.com/itsp/events/celloFest.xml");

// Create the new connection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

// Set up some things on the connection...
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);

// and connect!
urlConnection.connect();


// Set the path where we want to save the file.
// In this case, save it on the root directory of the
// SD card "\"
Log.d("xml download," , "just before downloading");
File SDCardRoot = Environment.getExternalStorageDirectory();

// Create a new file, specifying the path, and the filename,
// Chich we want to save the file as.
Log.d("xml download" , "Sd card root found");
File file = new File(SDCardRoot,"cellofest.xml");
Log.d("xml download" ,"fest.xml created.");

//this will be used to write the downloaded data into the file we created
FileOutputStream fileOutput = new FileOutputStream(file);

4 个答案:

答案 0 :(得分:0)

您必须先创建一个文件,然后才能使用它。快速修复如下。

// Create a new file, specifying the path, and the filename,
// Chich we want to save the file as.
Log.d("xml download" , "Sd card root found");
// File file = new File(SDCardRoot,"cellofest.xml");
File file = File.createTempFile("cellofest", "xml", SDCardRoot);
Log.d("xml download" ,"fest.xml created.");

此实例化创建您的File变量。

答案 1 :(得分:0)

您似乎没有SD卡的写入权限。确保在AndroidManifest.xml中声明了如下所示的权限:

    <manifest ...>

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

    </manifest>

如果您已经声明了,请确认您已将SD卡插入设备&amp;安装。然后它应该按预期工作。

答案 2 :(得分:0)

我确实设置了权限。 我找到了修复。 我的SD卡有200Mb的内存,这可能太多了。 把它改成了20Mb,它现在正在工作。

答案 3 :(得分:0)

String responseString = RequestMgr.GetWebSource("http://www.test.com/itsp/events/celloFest.xml");
File localConfigfile = new File(
                            Environment.getExternalStorageDirectory()
                                    + "cellofest.xml");
    if (localConfigfile.exists()) {
                        localConfigfile.delete();
    FileOutputStream out = new FileOutputStream(localConfigfile);
                            out.write(responseString.getBytes());
                            out.flush();
}

RequestMgr.GetWebSource只是我自己的函数,用于从URL获取响应。所以它不是java函数。

public static String GetWebSource(String url)
{
    HttpClient httpclient1 = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    //
    HttpResponse response = null;
    try {
        response = httpclient1.execute(httpGet);
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //StatusLine statusLine = response.getStatusLine();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            response.getEntity().writeTo(out);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    return out.toString();
}