如何将图像文件从URL下载到ByteArray?

问题描述:

下面是我的代码:如何将图像文件从URL下载到ByteArray?

private byte[] downloadImage(String image_url) { 
      byte[] image_blob = null; 
      URL _image_url = null; 
      HttpURLConnection conn = null; 
      InputStream inputStream = null; 
      try { 
       _image_url = new URL(image_url); 
      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } 
      try { 
       conn = (HttpURLConnection) _image_url.openConnection(); 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      conn.setDoInput(true); 
      try { 
       conn.connect(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      conn.setUseCaches(false); 
      try { 
       inputStream = conn.getInputStream(); 
       inputStream.read(image_blob); 

      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } finally { 
       try { 
        inputStream.close(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       conn.disconnect(); 
      } 
      return image_blob; 
     } 

我所试图做的是获取图像的字节数组。在包裹中使用它将其转移到其他活动。

使用此代码会报告NullPointerException。任何人都可以说出了什么问题吗?

+0

你介意告诉我们这行的NPE发生?并且请将完整的堆栈追踪添加到您的问题中? – Codo 2012-07-29 09:14:57

你可能想尝试这样的:

DefaultHttpClient client = new DefaultHttpClient(); 
HttpGet request = new HttpGet(imageUrl); 
HttpResponse response = client.execute(request); 
HttpEntity entity = response.getEntity(); 
int imageLength = (int)(entity.getContentLength()); 
InputStream is = entity.getContent(); 

byte[] imageBlob = new byte[imageLength]; 
int bytesRead = 0; 
while (bytesRead < imageLength) { 
    int n = is.read(imageBlob, bytesRead, imageLength - bytesRead); 
    if (n <= 0) 
     ; // do some error handling 
    bytesRead += n; 
} 

顺便说一句:该抛出NullPointerException造成的,因为image_blob为空。您需要先分配数组,然后才能将数据读入数组中。

+0

这似乎适用于某些文件,但其他一些文件不以此方式下载。似乎由于某种原因,并不总是可以事先知道内容的长度...... – Bob 2014-02-05 11:29:28

然后发送图像,你可以发送缓存下载的图像的路径。您可以使用此方法来验证图像路径并将图像下载到本地路径中。

private String createLocal(String surl) {  
    URL url; 
    try { 


     url = new URL(surl); 


     String tempname=String.valueOf(surl.hashCode()); 

     File root=getCacheDir(); 

     File localfile=new File(root.getAbsolutePath()+"/"+tempname); 

     localfile.deleteOnExit(); 
     if(!localfile.exists()){ 
      InputStream is=url.openStream(); 
      OutputStream os = new FileOutputStream(localfile); 
      CopyStream(is, os); 
      os.close(); 
     } 
     return localfile.getAbsolutePath(); 
    } catch (Exception e){ 
     return null; 
    } 
} 
public static void CopyStream(InputStream is, OutputStream os) { 
    final int buffer_size=1024; 
    try { 
     byte[] bytes = new byte[buffer_size]; 
     for(;;) { 
      int count=is.read(bytes, 0, buffer_size); 
      if(count == -1) 
       break; 
      os.write(bytes, 0, count); 
     } 
    } 
    catch(Exception ex){} 
} 

你的byte [] image_blob为空,则必须像新的足够的空间,然后再使用它:

image_blob = new byte[enough]; 
inputStream.read(image_blob); 

public static byte[] getByteArray(String url) throws IOException { 
    InputStream inputStream = (InputStream) new URL(url).getContent(); 
    return IOUtils.toByteArray(inputStream); 
}