Eoin
Eoin

Reputation: 4066

Simplify getting a bitmap from Uri/Url

Im looking for some help refactoring some code. I have these methods for getting a bitmap, they do something similar where they decode an input stream into a bitmap. I have to surround the opening of the input stream in a try/catch with a finally on the end. I noticed that these methods share a lot in common and Id like to refactor it so I only have to write the try/catch only once.

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

I thought about writing it something like this but I not sure it makes it anymore readable. Any suggestions to improve this?

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

Upvotes: 2

Views: 773

Answers (1)

kfmes
kfmes

Reputation: 110

try glide or picasso.

I'm using glide. link here https://github.com/bumptech/glide

and see https://github.com/bumptech/glide/wiki more information.

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

Upvotes: 2

Related Questions