2011-11-09 2 views
0

나는 때때로 오류없이 null을 반환하고, 때로는 그것을 작동합니다, 그러나이 방법다운로드 비트 맵 반환 널 가끔

public Bitmap downloadFile(String fileUrl){ 
    URL myFileUrl =null;   
    try { 
     myFileUrl= new URL(fileUrl); 
    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    try { 
     HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection(); 
     conn.setDoInput(true); 
     conn.setReadTimeout(500000000); 
     conn.connect(); 
     InputStream is = conn.getInputStream(); 
     BitmapFactory.Options options=new BitmapFactory.Options(); 
     options.inSampleSize = 8; 

     return BitmapFactory.decodeStream(is,null,options); 

    } catch (IOException e) { 
     Log.e("log_tag", " Download image failed"+e.getMessage()); 
     e.printStackTrace(); 
    } 
    return null; 
} 

를 사용하여 웹에서 이미지를 검색하는 몇 가지 코드가 있습니다.

시간 초과를 많이 설정하려고 시도했지만 여전히 오류가 없습니다.

+0

그것은 그 코드가 null을 반환 할 수있는 유일한 방법처럼 보인다 io 예외가있는 경우 스택 추적을 보시겠습니까? –

+0

나는 예외를 잡으려고했지만 아무런 오류도 반환하지 않습니다. null – ericlee

+0

http://stackoverflow.com/questions/3802820/bitmapfactory-decodestream-always-returns-null-and-skia-decoder-shows -decode-ret가 버그 인 것 같습니다. – ericlee

답변

1

catch 절에서이 코드를 사용해보십시오. 오류가있는 토스트 메시지가 표시됩니다. 당신은 아마 항상 나가는 것이 아닙니다. 높은 곳

catch(IOException e) { 
Log.e("IOException", e.getMessage()); 

// 토스터 ----------------- //

Context context = getApplicationContext(); 
CharSequence text = "IOException: " + e.getMessage(); 
int dur = Toast.LENGTH_LONG; 
Toast.makeText(context, text, dur).show(); 
+0

에서 위치 geopoints를 검색 할 때 동일한 오류를 처리 중이므로 어떤 오류도 catch하지 않습니다. – ericlee

+0

suc * & catch/O 연결할 수 없을 때 해당 블록과 함께 "서비스 예외". – apesa

관련 문제