繁体   English   中英

将.gif保存到SD卡

[英]Save .gif to sd card

我仍然是Android编程的新手,我想做点什么,

  1. 从网址下载.gif
  2. 将下载的.gif保存到我的SD卡中
  3. 用滑行来称呼它

当我下载.gif并将其写入sdcard时,它不可读。 并且大小大于原始文件。

这是我使用的代码:

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");

之前。

baos.write(img, 0, current); 表示写入current零( img current字节)。

尝试将行更改为baos.write(current);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM