2016-02-29 67 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