为什么从web服务器下载照片后会引发NullPointerException ?


Why is the NullPointerException raised after downloading a photo from a webserver?

我正在从web服务器下载一张照片。以下是在网页url

中显示照片的PHP代码:
<?php
$rep = $_GET['dossierClient'];
    if (file_exists($rep))
    {
        $myDirectory = opendir($rep);
        while($entryName = readdir($myDirectory)) {
            $dirArray[] = $entryName;
        }
        closedir($myDirectory);
        $indexCount = count($dirArray);
        sort($dirArray);
        for($index=0; $index < $indexCount; $index++) 
        {
            if (substr("$dirArray[$index]", 0, 1) != ".")
            {
                print("<img src=$rep/$dirArray[$index] />");
                //print("'n");
            }
        }
    }
?>

在我的javaME代码中,我从PHP页面下载:

Image tmpImage = downloadImage("http://192.168.1.123/imfmobile/photoj2meupload/downloadphoto.php?dossierClient="+photoDirectory);
Image thumbImage = createThumbnail(tmpImage);
Button thumbButton = new Button(thumbImage);
thumbButton.setUIID("btnPhotoThumb");
thumbButton.addActionListener(this);
vButtonPhotos.addElement(thumbButton);
addThumbButton(thumbButton);
revalidate();

这里Image引用了一个LWUIT图像。downloadadimage()方法的代码是:

private Image downloadImage(String url) throws IOException
    {
        Image img = null;
        byte[] rawImg = null;
        try
        {
            String imageData = getDataFromUrl(url);
            rawImg = imageData.getBytes();
            putPhotoToPhone(rawImg);
            img = Image.createImage(rawImg, 0, rawImg.length );
        }
        catch(Exception e1) {
            e1.printStackTrace();
        }
        return img;
    }
    public String getDataFromUrl(String url) throws IOException {
        StringBuffer b = new StringBuffer();
        InputStream is = null;
        HttpConnection c = null;
        long len = 0 ;
        int ch = 0;
        c = (HttpConnection)Connector.open(url);
        is = c.openInputStream();
        len = c.getLength();
        if( len != -1)
        {
            for(int i =0 ; i < len ; i++ )
            {
                if((ch = is.read()) != -1)
                {
                    b.append((char) ch);
                }
            }
        }
        else
        {
            while ((ch = is.read()) != -1)
            {
                len = is.available() ;
                b.append((char)ch);
            }
        }
        is.close();
        c.close();
        return b.toString();
    }
private void putPhotoToPhone(byte[] rawImg)
    {
        FileConnection fcDir, fcFile;
        int photoId, photoNextCounter;
        String fileName;
        OutputStream os;
        if (rawImg != null)
        {
            try {
                fcDir = (FileConnection) Connector.open("file:///"+pRoot+photoDirectory+"/", Connector.READ_WRITE);
                if(!fcDir.exists())
                    fcDir.mkdir();
                if (vPhotosName.isEmpty())
                    photoNextCounter = 1;
                else
                    photoNextCounter = 1;
                    //photoNextCounter = getNextImageCounter(fcDir, String.valueOf(backForm.vPhotosName.elementAt(backForm.vPhotosName.size()-1)));
                fileName = "photo_downloaded_" + photoNextCounter + ".png";
                fcFile = (FileConnection) Connector.open("file:///"+pRoot+photoDirectory+"/"+fileName, Connector.READ_WRITE);
                if(!fcFile.exists())
                    fcFile.create();
                os = fcFile.openOutputStream();
                os.write(rawImg);
                os.close();
                fcFile.close();
                fcDir.close();
                try {
                    photoId = rsImage.addRecord(rawImg, 0, rawImg.length);
                    vRawPhotoIDs.addElement(new Integer(photoId));
                }
                catch (RecordStoreException ex) {}
                vPhotosName.addElement(fileName);
            }
            catch (Exception e) {}
        }
    }

所以问题是在createThumbnail方法:当应用程序到达这个方法时抛出NullPointerException。甚至当我尝试在手机设备中打开照片时,我得到一个"无效格式"错误,照片不显示。

下面是createThumbnail()的代码:
public Image createThumbnail(Image image) {
        int sourceWidth = image.getWidth();
        int sourceHeight = image.getHeight();
        int thumbWidth = 50;
        int thumbHeight = -1;
        if (thumbHeight == -1) {
            thumbHeight = thumbWidth * sourceHeight / sourceWidth;
        }
        Image thumb = image.scaled(thumbWidth, thumbHeight);
        return thumb;
    }
那么如何使这个下载正确呢?

很可能您的映像只是下载失败,进一步导致传递给createThumbnail的参数为null,并且当您在该空参数上调用image.getWidth()时导致NPE。

如果您检查控制台(我猜您使用模拟器进行调试),您可能会注意到来自 downloadadimage方法中的e1.printStackTrace();的堆栈跟踪。检查它告诉你的信息,并试着找出下载

哪里出错了。
相关文章:
  • 没有找到相关文章