嘿傢伙有人能告訴我爲什麼我不能保存這個文件到外部SD?你能檢查我的代碼嗎?Android保存到外部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();
}
}
你得到的錯誤是什麼? – OceanBlue 2011-04-29 20:56:32
在這種情況下,很高興看到錯誤和/或堆棧跟蹤。 – Devunwired 2011-04-29 22:07:06