2

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;
}
Eoin
  • 4,050
  • 2
  • 33
  • 43

1 Answers1

2

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);
}
kfmes
  • 110
  • 4
  • Thanks for the answer, I use glide myself but in this case I dont need to load into an imageview. I need to directly access the bitmaps – Eoin Aug 17 '15 at 06:31
  • read this just downloading image. http://stackoverflow.com/questions/27640307/android-glide-how-to-download-and-cache-bitmaps . – kfmes Aug 17 '15 at 06:36
  • this is good, but the only problem is I still have to give the width and height but I dont know what these will be – Eoin Aug 17 '15 at 06:44
  • Callback returns bitmap. Bitmap class has height and width property. Can't use it? – kfmes Aug 17 '15 at 06:57
  • doesnt seem to be able to do it without passing the width and height first – Eoin Aug 17 '15 at 07:11