gpt4 book ai didi

java - Android保存到外部SD卡?

转载 作者:太空狗 更新时间:2023-10-29 13:40:55 24 4
gpt4 key购买 nike

为什么我无法将此文件保存到外部 sd?你能检查我的代码吗?

 public void Download()
{
try {
//this is the file you want to download from the remote server
String path ="http://mozilla.cdn.leaseweb.com/firefox/releases/4.0.1/win32/en-US/Firefox%20Setup%204.0.1.exe";
//this is the name of the local file you will create
String targetFileName;
boolean eof = false;
URL u = new URL(path);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();

//String svto = Environment.getExternalStorageState().toString();
File path1 = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);

path1.mkdirs();
FileOutputStream f = new FileOutputStream(new File(path1+"/fox.exe"));
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ( (len1 = in.read(buffer)) != -1 ) {
f.write(buffer,0, len1);
}
f.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

最佳答案

如果您在 AndroidManifest.xml 中没有以下权限,此操作将失败文件:

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

确保将它们放在顶级 <manifest> 中标签,而不是 <application>指定您的 Activity 的标签。

关于java - Android保存到外部SD卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5837300/

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