我有一個問題,試圖讓我的android手機.png圖像。 我現在這個地步:如何獲取.png到Android手機(external_sd)
URL url = new URL ("http://oranjelan.nl/oranjelan-bg.png");
InputStream input = url.openStream();
try {
String storagePath = Environment.getExternalStorageDirectory();
OutputStream output = new FileOutputStream (storagePath + "/oranjelangbg.png");
try {
byte[] buffer = new byte[1000000];
int bytesRead = 0;
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
output.write(buffer, 0, bytesRead);
}
} finally {
output.close();
}
} finally {
input.close();
}
但我得到以下錯誤:STRING storagePath = Environment.getExternalStorageDirectory();壓縮機說不能將文件轉換爲字符串。
然後,如果我愚弄了一下,試試這段代碼:
URL url;
void setup() {
try {
URL url = new URL ("http://oranjelan.nl/oranjelan-bg.png");
InputStream input = url.openStream();{
try {
String storagePath = Environment.getExternalStorageDirectory().getName();
OutputStream output = new FileOutputStream (storagePath + "/oranjelangbg.png");
try {
byte[] buffer = new byte[1000000];
int bytesRead = 0;
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
output.write(buffer, 0, bytesRead);
}
} finally {
output.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
} finally {
try {
input.close();
} catch (IOException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
}
} catch (MalformedURLException ex) {
throw new RuntimeException(ex);
} catch (IOException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
}
那麼我的Android凍結當我啓動應用程序。
有沒有人有任何想法?
很多很多謝謝你的回覆,我現在必須退出,並且會在星期一回來,然後回覆並查看此信息。 – 2012-07-06 15:31:32
嘗試下載不同線程中的圖像。它會克服凍結問題.. – AAnkit 2012-07-06 15:36:10
非常感謝你們,我想我現在就能得到它,所有的芒果都很好!並幫助我很多! – 2012-07-09 07:48:06