2017-07-25 33 views
0

我有2案件BitmapFactory解碼流returing空,而使用InputStream讀入

第一種情況

在這種情況下,圖像顯示,但進度對話框不顯示

URL url = new URL(params[0]); 
connection = (HttpsURLConnection) (url.openConnection()); 
connection.setRequestMethod("GET"); 
connection.connect(); 

int fileSize = connection.getContentLength()/1000; 

stream = connection.getInputStream(); 
image = BitmapFactory.decodeStream(stream); 


    while((b = stream.read(data))!=-1) 
    { 
    total += b/1000; 
    publishProgress(String.valueOf(total)+" kb/"+String.valueOf(fileSize)+"kb"); 
    } 


return image; 

登錄

D/ImageDownload: doInBackground: [email protected] 

第二種情況 在這種情況下進度對話框中顯示,但圖像沒有顯示

URL url = new URL(params[0]); 
connection = (HttpsURLConnection) (url.openConnection()); 
connection.setRequestMethod("GET"); 
connection.connect(); 

int fileSize = connection.getContentLength()/1000; 

stream = connection.getInputStream(); 

while((b = stream.read(data))!=-1) 
{ 
    total += b/1000; 
    publishProgress(String.valueOf(total)+" kb/"+String.valueOf(fileSize)+"kb"); 
} 

image = BitmapFactory.decodeStream(stream); 
Log.d(TAG, "doInBackground: "+String.valueOf(image)); 


return image; 

登錄

D/ImageDownload: doInBackground: null 

回答

0

我做到了3小時掙扎

使用後ByteArrayOutputStream

ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
    byte []data = new byte[1024]; 
    int b; 
    while((b = stream.read(data))!=-1) 
    { 
     total += b/1000; 
     publishProgress(String.valueOf(total)+" kb/"+String.valueOf(fileSize)+"kb"); 
     bos.write(data,0,b); 
    } 

byte []data = new byte[1024] 
InputStream in = new ByteArrayInputStream(content); 
image = BitmapFactory.decodeStream(in);