我正在寻找一些重构一些代码的帮助。我有这些获取位图的方法,它们在将输入流解码为位图的过程中执行类似的操作。我必须在try / catch中将输入流的开头括起来,并在最后插入finally。我注意到这些方法有很多共同点,并且我想对其进行重构,因此我只需要编写一次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;
}

最佳答案

尝试滑翔或毕加索。

我正在滑行。在此处链接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);
}

09-07 01:39