Android: bitmapfactory.decodestream returns null

前端 未结 5 1910
鱼传尺愫
鱼传尺愫 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 07:58

    using the following code iam able to download an image from the url

    String IMAGE_URL = "http://www.kolkatabirds.com/rainquail8vt.jpg";
                //where we want to download it from
                URL url;
                try {
                    url = new URL(IMAGE_URL);
    
                    //open the connection
                    URLConnection ucon = url.openConnection();
    
                    //buffer the download
                    InputStream is = ucon.getInputStream();
                    BufferedInputStream bis = new BufferedInputStream(is,1024);
    
                    //get the bytes one by one
                    int current = 0;
    
                    while ((current = bis.read()) != -1) {
                        baf.append((byte) current);
                    }
                    //convert it back to an image
                    ByteArrayInputStream imageStream = new ByteArrayInputStream(baf.toByteArray());
                    Bitmap theImage = BitmapFactory.decodeStream(imageStream);
                    img.setImageBitmap(theImage);
    

提交回复
热议问题