Android: bitmapfactory.decodestream returns null

前端 未结 5 1939
鱼传尺愫
鱼传尺愫 2020-12-19 07:33

I have tried to get the bitmap image from the path of the image. But BitmapFactory.decodeStream returns null value.

Code:

U         


        
5条回答
  •  太阳男子
    2020-12-19 08:13

    public Bitmap getBitmapFromUrl(String url)
    {
    Bitmap bm = null;
    InputStream is = null;
    BufferedInputStream bis = null;
    try 
    {
        URLConnection conn = new URL(url).openConnection();
        conn.connect();
        is = conn.getInputStream();
        bis = new BufferedInputStream(is, 8192);
        bm = BitmapFactory.decodeStream(bis);
    }
    catch (Exception e) 
    {
        e.printStackTrace();
    }
    finally {
        if (bis != null) 
        {
            try 
            {
                bis.close();
            }
            catch (IOException e) 
            {
                e.printStackTrace();
            }
        }
        if (is != null) 
        {
            try 
            {
                is.close();
            }
            catch (IOException e) 
            {
                e.printStackTrace();
            }
        }
    }
    return bm;
    }
    

    Dont forget to call this within a thread (not Main thread)

提交回复
热议问题