我已经把头发拉了一段时间了.以下方法应该下载文件,并将其保存到硬盘驱动器上指定的位置.
private static void saveImage(Context context, boolean backgroundUpdate, URL url, File file) {
if (!Tools.checkNetworkState(context, backgroundUpdate))
return;
// Get the image
try {
// Make the file
file.getParentFile().mkdirs();
// Set up the connection
URLConnection uCon = url.openConnection();
InputStream is = uCon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
// Download the data
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
// Write the bits to the file
OutputStream os = new …Run Code Online (Sandbox Code Playgroud)