简化从Uri / Url获取位图

时间:2015-08-17 05:56:14

标签: android android-bitmap

我正在寻找帮助重构一些代码的帮助。我有这些获取位图的方法,它们在将输入流解码为位图时执行类似的操作。我必须在try / catch中包围输入流的开头,最后结束。我注意到这些方法有很多共同点,我喜欢重构它所以我只需要编写一次try / catch。

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;
}

1 个答案:

答案 0 :(得分: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);
}