2015-08-17 73 views
2

我正在尋找一些幫助重構一些代碼。我有這些獲取位圖的方法,他們在將輸入流解碼爲位圖時做了類似的操作。我必須在try/catch中結尾輸入流的開頭。我注意到這些方法有很多共同點,並且我想重構它,所以我只需要編寫try/catch一次。簡化從Uri/Url獲取位圖

public static Bitmap fromUri(@NonNull Context context, @NonNull Uri uri) { 
    InputStream inputStream = null; 
    try { 
     inputStream = context.getContentResolver().openInputStream(uri); 
     return BitmapFactory.decodeStream(inputStream, null, new BitmapFactory.Options()); 
    } catch (FileNotFoundException e) { 
     return null; 
    } catch (NullPointerException e) { 
     return null; 
    } finally { 
     try { 
      if (inputStream != null) { 
       inputStream.close(); 
      } 
     } catch (IOException e) { 
      // ignore 
     } 
    } 
} 

public static Bitmap fromURL(@NonNull String src, @Nullable BitmapFactory.Options options) { 
    InputStream inputStream = null; 
    try { 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpGet request = new HttpGet(src); 
     HttpResponse response = httpClient.execute(request); 
     inputStream = response.getEntity().getContent(); 
     return BitmapFactory.decodeStream(inputStream, null, options); 
    } catch (Exception e) { 
     return null; 
    } finally { 
     if (inputStream != null) { 
      try { 
       //todo test that input stream is closed 
       inputStream.close(); 
      } catch (IOException e) { 
       // ignore 
      } 
     } 
    } 
} 

我想過寫這樣的東西,但我不確定它是否使它變得可讀。任何建議,以改善這一點?

public static Bitmap fromUri(@NonNull Context context, @NonNull Uri uri) { 
    InputStream inputStream = getInputStream(context, uri); 
    return BitmapFactory.decodeStream(inputStream, null, new BitmapFactory.Options()); 
} 

public static Bitmap fromURL(@NonNull String src, @Nullable BitmapFactory.Options options) { 
    InputStream inputStream = getInputStream(null, src); 
    return BitmapFactory.decodeStream(inputStream, null, options); 
} 

public static InputStream getInputStream(@Nullable Context context, @NonNull Object source){ 
    InputStream inputStream = null; 
    try { 
     if(source instanceof String){ 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpGet request = new HttpGet(String.valueOf(source)); 
      HttpResponse response = httpClient.execute(request); 
      inputStream = response.getEntity().getContent(); 
     } else if(source instanceof Uri){ 
      inputStream = context.getContentResolver().openInputStream((Uri) source); 
     } 
    } catch (Exception e) { 
     return null; 
    } finally { 
     if (inputStream != null) { 
      try { 
       //todo test that input stream is closed 
       inputStream.close(); 
      } catch (IOException e) { 
       // ignore 
      } 
     } 
    } 

    return inputStream; 
} 

回答

2

嘗試滑行或畢加索。

我正在使用滑翔。鏈接這裏https://github.com/bumptech/glide

並參見https://github.com/bumptech/glide/wiki更多信息。

//from glide document 
public void onCreate(Bundle savedInstanceState) { 
    ... 
    ImageView imageView = (ImageView) findViewById(R.id.my_image_view); 

    Glide.with(this).load("http://goo.gl/gEgYUd").into(imageView); 
} 
+0

感謝您的答案,我使用滑翔自己,但在這種情況下,我不需要加載到imageview。我需要直接訪問位圖 – Eoin

+0

閱讀這只是下載圖像。 http://stackoverflow.com/questions/27640307/android-glide-how-to-download-and-cache-bitmaps。 – kfmes

+0

這是好的,但唯一的問題是我還是要給出寬度和高度,但我不知道這些會是什麼 – Eoin