我还是Android编程的新手, 我想做点什么,
.gif
.gif
保存到我的SD卡当我下载.gif
并将其写入SD卡时,它无法读取。
并且大小比原始文件大。
这是我使用的代码:
URL url = new URL(imgUrl + params[0]);
File file = new File(path + params[0]);
long startTime = System.currentTimeMillis();
URLConnection ucon = url.openConnection();
Log.d(TAG, "on do in background, url open connection");
InputStream is = ucon.getInputStream();
Log.d(TAG, "on do in background, url get input stream");
BufferedInputStream bis = new BufferedInputStream(is);
Log.d(TAG, "on do in background, create buffered input stream");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Log.d(TAG, "on do in background, create buffered array output stream");
byte[] img = new byte[1024];
int current = 0;
Log.d(TAG, "on do in background, write byte to baos");
while ((current = bis.read()) != -1) {
baos.write(img, 0, current);
}
Log.d(TAG, "on do in background, done write");
Log.d(TAG, "on do in background, create fos");
FileOutputStream fos = new FileOutputStream(file);
fos.write(baos.toByteArray());
Log.d(TAG, "on do in background, write to fos");
fos.flush();
fos.close();
is.close();
Log.d(TAG, "on do in background, done write to fos");
之前的.x。
答案 0 :(得分:0)
baos.write(img, 0, current);
表示从current
写入current
个零(img
字节)。
尝试将行更改为baos.write(current);
。