我在尝试将 .png 图像发送到我的 Android 手机时遇到问题。我现在这么远:
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 冻结了。
有没有人有任何想法?