2016-02-29 132 views
0

我在Android的编程, 还是新的,我想要做的事情,从URL保存.gif若要SD卡

  1. 下载.gif
  2. 保存该下载.gif到我的SD卡
  3. 叫它使用滑行

当我下载.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"); 

THX之前。

回答

0

baos.write(img, 0, current);意味着写current零(current字节从img)。

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

+0

thx Mike it; s really working。 awsome:D – HartzDev