How to handle try catch exception android

后端 未结 5 2116
滥情空心
滥情空心 2020-12-31 11:24

I am using a method getBitmap to display images. As I am using this as a method,if it returns bitmap display an image but if it returns null,catch an exception. But if url e

5条回答
  •  忘掉有多难
    2020-12-31 12:01

    Throw the exceptions from the getBitmap method instead and let the client (Activity) handle the exceptions, in this case either you receive a Bitmap or an exception and can skip the return null bitmap and do the according "default bitmap loading" in the catch blocks, for the exception/error case instead (since this is the null case now).

    public Bitmap getBitmap(final String src) throws FileNotFoundException,
            OutOfMemoryError, IOException, MalformedURLException {
        URL url = new URL(src);
        URLConnection connection = url.openConnection();
        InputStream input = connection.getInputStream();
        return BitmapFactory.decodeStream(input);
    }
    

提交回复
热议问题