gpt4 book ai didi

java - android fileoutputstream 找不到文件异常

转载 作者:太空宇宙 更新时间:2023-11-04 14:46:58 25 4
gpt4 key购买 nike

我正在尝试从 url 下载 xml。但我收到java.io FileNotFound sdcard.cellofest.xml Permission Denied。最后一行正在生成错误。我检查了 Google 并找到了很多修复方法,但没有一个解决了我的问题。

我在 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);

最佳答案

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

// 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 变量。

关于java - android fileoutputstream 找不到文件异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24291428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com