android下载文件到本地sdcard android存储thread 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /** * 把网络上的文件保存到sd卡 * @param url 完整的可访问的url * @param file 文件 * @return boolean */ public boolean saveUrlFile(String url, File file) throws Exception { HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); fileSize = conn.getContentLength(); InputStream input = conn.getInputStream(); if (null != input) { copyFile(input, file); conn.disconnect(); return true; } return false; }/** * 复制文件 * @param input 输入流 * @param newFile 新文件 * @throws Exception */ public void copyFile(InputStream input, File newFile) throws Exception { if (null != progress) {progress.setMax(fileSize);} OutputStream output = new FileOutputStream(newFile); byte[] buffer = new byte[1024]; int i = 0, total = 0; while ((i = input.read(buffer)) != -1) { output.write(buffer, 0, i); total += i; if (null != progress) {progress.setProgress(total);} } output.flush();output.close();input.close(); }带进度条的文件下载 progress 是什么? 进度条呀,上传下载必须要有进度条,否则很不友好的private int fileSize = 0; private ProgressDialog progress = null;gressDialog = new ProgressDialog(this); gressDialog.setCanceledOnTouchOutside(true); gressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); gressDialog.setMessage("正在下载文件..."); gressDialog.show(); 我发现你 HttpURLConnection 用完不关闭 ,还有流用完随手关闭 好习惯 亲 增加权限 在你的AndroidMainifest.xml 里在"<application "前面增加这几句话 <!-- 在SDCard中创建与删除文件权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!-- 往SDCard写入数据权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 现在问题是真机测试也下载不下来,会很慢,然后就提示“android应用无响应”。求解 真机应该是可以对SD卡操作的 我用过的很慢 无响应的话 貌似是不是哪里死循环了呢? 比如这里 你打印一下调试信息看看啊while (true) { int temp = input.read(buffer, 0, buffer.length); if (temp == -1) { break; } output.write(buffer, 0, temp); } 你试试在异步里面下载文件吧 http://jackyrong.iteye.com/blog/1336299 android4.0下, 如何弹出关机 对话框? android 富文本编辑 android开发office文档阅读器(求思路) Android源代码所在的目录在什么地方 请教高手一个很怪异的问题,希望能帮助解决?????? 新学android遇到几个问题,急求解决??? 看个手机! 请大家帮我看一下这段代码的错误在了哪了,运行就提示空指针 新手请教个问题,有关cocos2d-x 把通讯录里面的联系人全部查找出来,为什么不行? 关于调用google map的问题!!!! barcode
* 把网络上的文件保存到sd卡
* @param url 完整的可访问的url
* @param file 文件
* @return boolean
*/
public boolean saveUrlFile(String url, File file) throws Exception {
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
fileSize = conn.getContentLength();
InputStream input = conn.getInputStream();
if (null != input) {
copyFile(input, file);
conn.disconnect();
return true;
}
return false;
}
/**
* 复制文件
* @param input 输入流
* @param newFile 新文件
* @throws Exception
*/
public void copyFile(InputStream input, File newFile) throws Exception {
if (null != progress) {progress.setMax(fileSize);}
OutputStream output = new FileOutputStream(newFile);
byte[] buffer = new byte[1024];
int i = 0, total = 0;
while ((i = input.read(buffer)) != -1) {
output.write(buffer, 0, i);
total += i;
if (null != progress) {progress.setProgress(total);}
}
output.flush();output.close();input.close();
}带进度条的文件下载
private int fileSize = 0;
private ProgressDialog progress = null;gressDialog = new ProgressDialog(this);
gressDialog.setCanceledOnTouchOutside(true);
gressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
gressDialog.setMessage("正在下载文件...");
gressDialog.show();
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
真机应该是可以对SD卡操作的 我用过的
很慢 无响应的话 貌似是不是哪里死循环了呢? 比如这里 你打印一下调试信息看看啊
while (true) {
int temp = input.read(buffer, 0, buffer.length);
if (temp == -1) {
break;
}
output.write(buffer, 0, temp);
}