2

以下是我的代码:

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

4 回答 4

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为空而引起的。您需要先分配数组,然后才能将数据读入其中。

于 2012-07-29T09:26:31.620 回答
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){}
}
于 2012-07-29T09:27:45.677 回答
0

您的 byte[] image_blob 为空,在使用它之前,您必须像这样新建足够的空间:

image_blob = new byte[enough];
inputStream.read(image_blob);
于 2012-07-29T09:31:11.413 回答
-1
public static byte[] getByteArray(String url) throws IOException {
    InputStream inputStream = (InputStream) new URL(url).getContent();
    return IOUtils.toByteArray(inputStream);
}
于 2016-04-01T18:11:59.087 回答