来自URL的Android位图始终为null

我正在尝试从URL下载图像并将其转换为位图,但该行

Bitmap myBitmap = BitmapFactory.decodeStream(input); 

始终导致调试器跳到以下行

 return null; 

没有实际打印出堆栈跟踪,并且调试器中列出的变量中也不存在Exception变量。 我读了很多关于url可能没有实际导致图像问题,没有格式化图像之类的问题,但它仍然存在与我积极存在的硬编码图像相同的问题。

 public static Bitmap getBitmapFromURL(String src) { try { URL url = new URL( "http://www.helpinghomelesscats.com/images/cat1.jpg"); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setDoInput(true); connection.connect(); InputStream input = connection.getInputStream(); Bitmap myBitmap = BitmapFactory.decodeStream(input); return myBitmap; } catch (IOException e) { e.printStackTrace(); return null; } } 

由于似乎可能涉及manifest.xml文件,因此我已编辑添加到此处。

                    

以下代码适用于所有类型的图像。

  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(); } 

嗨,你可以在android表现文件中给出这2个pemssions

   

在您的活动中:

 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; } 

这适用于我,在搜索另一个问题时在此处找到它。 这样它就不会占用UI。 在ArrayAdapters中也能很好地工作。

用途:

 _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..."); 

Asynctask

 class DownloadImageTask extends AsyncTask { 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); } }