2013-02-12 3 views
1

갤러리에서 표시 할 인터넷에서 여러 이미지를 가져 오는 Android 애플리케이션을 프로그래밍하고 있습니다. 이미지는 먼저 FragmentStatePagerAdapter로 설정된 ViewPager 안의 프래그먼트로 페치되기 전에 디스크에 저장됩니다.Android에서 이미지 다운로드/저장 오류를 해결하는 방법

문제는 내가 가지고있는 문제는 세 번째 이미지를 다운로드 한 후 이후의 모든 이미지가 URL이 분명히 null 인 NullPointerException이 발생한다는 것입니다. 여기 내가 사용하는 downloadBitmap() 방법 : 세 번째 이미지가 다운로드 된 후

static Bitmap downloadBitmap(String url) { 
    final AndroidHttpClient client = AndroidHttpClient 
      .newInstance("Android"); 
    final HttpGet getRequest = new HttpGet(url); 

    try { 
     HttpResponse response = client.execute(getRequest); 
     final int statusCode = response.getStatusLine().getStatusCode(); 
     if (statusCode != HttpStatus.SC_OK) { 
      Log.w("ImageDownloader", "Error " + statusCode 
        + " while retrieving bitmap from " + url); 
      return null; 
     } 

     final HttpEntity entity = response.getEntity(); 
     if (entity != null) { 
      InputStream inputStream = null; 
      try { 
       inputStream = entity.getContent(); 
       final Bitmap bitmap = BitmapFactory 
         .decodeStream(inputStream); 
       return bitmap; 
      } finally { 
       if (inputStream != null) { 
        inputStream.close(); 
       } 
       entity.consumeContent(); 
      } 
     } 
    } catch (Exception e) { 
     // Could provide a more explicit error message for IOException or 
     // IllegalStateException 
     getRequest.abort(); 
     Log.w("ImageDownloader", "Error while retrieving bitmap from " 
       + url); 
    } finally { 
     if (client != null) { 
      client.close(); 
     } 
    } 
    return null; 
} 

는 다시,이 방법은 오류를 뱉어하기 시작 그 상태 "오류에서 비트 맵을 검색하는 동안"마지막에 덧붙여진 URL없이, 메서드에 null 또는 빈 문자열을 전달하고 있음을 나타냅니다. 그러나 Log.d ("URL", saidURL)를 사용하여 메서드를 실행하기 전에 통과 한 URL 문자열을 로깅하는 경우 URL이 완벽하게 유효 함을 나타냅니다. downloadBitmap() 코드는 다음 AsyncTask에서 호출됩니다.

class RetrieveImagesTask extends 
     AsyncTask<ArrayList<Magazine>, String, ArrayList<Magazine>> { 

    @Override 
    protected ArrayList<Magazine> doInBackground(
      ArrayList<Magazine>... arg0) { 

     Bitmap bitmap; 
     for (Magazine mag : arg0[0]) { 
      if (!new File(filesDir, mag.ID + ".jpg").exists()) 
       try { 
        FileOutputStream out = new FileOutputStream(filesDir + mag.ID 
          + ".jpg"); 
        Log.d("URL", mag.imageURL); 
        bitmap = downloadBitmap(mag.imageURL); 
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
     } 
     return arg0[0]; 
    } 

이 이상한 상태에 대한 도움이 필요하십니까? 메모리 부족으로 문제가 될 수 있습니까?

+0

는') (e.printStackTrace'에 캐치 부분을 변경하여 예외 사항을 게시하시기 바랍니다 수 없습니다. – Dinesh

+0

URL을 가져 오는 서버가 반 시간 동안 빈 URL 문자열을 제공하고 있음을 나타냅니다. – ccyellow

답변

0
static Bitmap downloadBitmap(String url) { 
final AndroidHttpClient client = AndroidHttpClient 
     .newInstance("Android"); 
final HttpGet getRequest = new HttpGet(url); 

try { 
    HttpResponse response = client.execute(getRequest); 
    final int statusCode = response.getStatusLine().getStatusCode(); 
    if (statusCode != HttpStatus.SC_OK) { 
     Log.w("ImageDownloader", "Error " + statusCode 
       + " while retrieving bitmap from " + url); 
     return null; 
    } 

    final HttpEntity entity = response.getEntity(); 
    if (entity != null) { 
     InputStream inputStream = null; 
     try { 
      inputStream = entity.getContent(); 


      final Bitmap bitmap; 

int h = 100; // height in pixels 
       int w = 100; // width in pixels 
       if (bitmap != null) { 
        bitmap = Bitmap.createScaledBitmap(
          BitmapFactory 
        .decodeStream(inputStream), h, w, true); 

      return bitmap; 
     } finally { 
      if (inputStream != null) { 
       inputStream.close(); 
      } 
      entity.consumeContent(); 
     } 
    } 
} catch (Exception e) { 
    // Could provide a more explicit error message for IOException or 
    // IllegalStateException 
    getRequest.abort(); 
    Log.w("ImageDownloader", "Error while retrieving bitmap from " 
      + url); 
} finally { 
    if (client != null) { 
     client.close(); 
    } 
} 
return null; 

}

관련 문제