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

时间:2012-07-29 09:12:21

标签: android image download

以下是我的代码:

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。任何人都可以说出了什么问题吗?

4 个答案:

答案 0 :(得分:3)

你可能想这样试试:

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为空。在将数据读入数据之前,需要先分配数组。

答案 1 :(得分:0)

而不是发送图像,您可以发送在缓存中下载的图像路径。您可以使用此方法来生成图像路径并将图像下载到本地路径。

    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){}
}

答案 2 :(得分:0)

你的byte [] image_blob为null,在你使用之前你必须有足够新的空间:

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

答案 3 :(得分:-1)

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