【发布时间】:2013-11-07 09:49:08
【问题描述】:
使用下面的代码并能够保存在应用程序缓存中但不能保存在 sd 卡中..在 sd 卡中保存的文件是 0 字节无法得到确切的错误.....提前谢谢
final FileOutputStream f = new FileOutputStream(directory);
URL u = new URL(fileURL);
final HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
Thread thread = new Thread(new Runnable() {
@SuppressWarnings("deprecation")
@Override
public void run() {
try {
c.connect();
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
int flag = 0;
while ((len1 = in.read(buffer)) != -1) {
f.write(buffer, 0, len1);
Log.d("Thread ", String.valueOf(len1));
}
f.close();
thread.start();
} catch (Exception e) {
Toast.makeText(this, "In exception", Toast.LENGTH_LONG).show();
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
【问题讨论】:
-
您可以阅读此链接 (stackoverflow.com/questions/5472226/…) 并对其进行自定义。
标签: java android file-io sd-card