Android 保存到外部 SD 卡吗?
为什么我无法将此文件保存到外部 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();
}
}
Why I cannot save this file to the external sd? Can you check over my code?
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的
AndroidManifest.xml
文件中没有以下权限,此操作将会失败:确保将它们放在顶级
标记中,不是指定您的活动的
标记。This operation will fail if you don't have the following permissions in your
AndroidManifest.xml
file:Make sure to put them within the top-level
<manifest>
tag, not the<application>
tag where your activities are specified.我之前也遇到过类似的问题。事实证明,我必须先在 SD 卡上手动创建目录,然后再调用诸如
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
之类的代码检查文件资源管理器上是否存在该目录的绝对路径。如果没有,请通过
adb shell
创建它,然后重试。这解决了我的问题,但不一定能解决您的问题,请尝试一下!I had a problem similar to this before. It turned out that I had to manually create the directory first on the SD card before calling code such as
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
Check if the absolute path exists on your file explorer for that directory. If it doesn't, then create it via
adb shell
and then give it a try again. This solved my problem, but it may not necessarily solve yours, give it a try though!