Android Bitmap from URL always null

心不动则不痛 提交于 2019-12-01 13:14:46

following code work for all type of images.

    try {

        URL url = new URL("http://www.helpinghomelesscats.com/images/cat1.jpg");
        InputStream in = url.openConnection().getInputStream(); 
        BufferedInputStream bis = new BufferedInputStream(in,1024*8);
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        int len=0;
        byte[] buffer = new byte[1024];
        while((len = bis.read(buffer)) != -1){
            out.write(buffer, 0, len);
        }
        out.close();
        bis.close();

        byte[] data = out.toByteArray();
        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        imageView.setImageBitmap(bitmap);
    }
    catch (IOException e) {
        e.printStackTrace();
    }

Hi you may give these 2 pemssions in the android manifeast file

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

In your activity:

url="http://www.helpinghomelesscats.com/images/cat1.jpg"
Bitmap bmp=readBitmapFromNetwork(url);



 public static Bitmap readBitmapFromNetwork(String imgurl) {
         URL url;
         Bitmap bmp = null;
         InputStream is = null;
           BufferedInputStream bis = null;
           System.out.println("image url ==========   "+imgurl);
        try {

            url=new URL(imgurl);
            System.out.println("url.getPath()"+url.getPath());          
                try {
               URLConnection conn = url.openConnection();
                    conn.connect();
                    is = conn.getInputStream();
                    bis = new BufferedInputStream(is);
                bmp = BitmapFactory.decodeStream(bis);
                } catch (MalformedURLException e) {                 
                    System.out.println("Bad ad URL");
                    e.printStackTrace();
                } catch (IOException e) {                   
                    System.out.println("Could not get remote ad image");
                    e.printStackTrace();
                }

        } catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
         finally {
                try {
                    if( is != null )
                        is.close();
                    if( bis != null )
                      bis.close();
                } catch (IOException e) {
                    System.out.println("Error closing stream.");
                    e.printStackTrace();
                }
            }   


           return bmp;
            }

This works for me, found it on here while searching out another issue. This way it's not holding up the UI. works very well in ArrayAdapters as well.

Useage:

_coverArt = (ImageView) row.findViewById(R.id.top_CoverArt);

new DownloadImageTask(_coverArt)
            .execute("Put your URL here Hint: make sure it's valid http://www.blah...");

The Asynctask

class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;

public DownloadImageTask(ImageView bmImage) {
    this.bmImage = bmImage;
}

protected Bitmap doInBackground(String... urls) {
    String urldisplay = urls[0];
    Bitmap mIcon11 = null;
    try {
        InputStream in = new URL(urldisplay).openStream();
        mIcon11 = BitmapFactory.decodeStream(in);
    } catch (Exception e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }
    return mIcon11;
}

protected void onPostExecute(Bitmap result) {
    bmImage.setImageBitmap(result);
}
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!