确保您的应用程序拥有“WRITE_EXTERNAL_STORAGE”权限,以便可以在设备中存储文件。
请检查您的下载目录是否存在。 如果不存在,则必须创建目录。 以下是代码示例:
File downloadFolder = new File(Environment.getExternalStorageDirectory() + "/Download");
if (!downloadFolder.exists()) {
downloadFolder.mkdirs();
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "filename");
URL url = new URL(downloadUrl);
URLConnection connection = url.openConnection();
connection.connect();
int lengthOfFile = connection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(downloadFolder + "/" + fileName);
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
如果您遵循上述步骤,您的应用程序应该能够成功将文件下载到 Android 下载文件夹中。
上一篇:Android下载速度慢
下一篇:android下装ubuntu