开发者

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

I am downloading a photo from a webserver. Here is the PHP code to display the photo in the web page url :

<?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) != ".")
            {
                p开发者_运维技巧rint("<img src=$rep/$dirArray[$index] />");
                //print("\n");
            }
        }
    }
?>

In my javaME code I make a download from the PHP page :

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

Here Image is referencing a LWUIT Image. The code of the downloadImage() method is :

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

So the problem is at the createThumbnail method : the NullPointerException is raised when the application reaches this method. Even when I try to open the photo in the phone device then I get an "invalid format" error and the photo is not displayed.

Here is the code of 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;
    }

So how to make this download correct ?


Most likely your image simply fails to download, further causing parameter passed to createThumbnail to be null and NPE when you invoke image.getWidth() on that null parameter.

If you check the console (I guess you debug with emulator), you'll probably notice stack trace that comes from e1.printStackTrace(); in downloadImage method. Check the information it tells you and try to figure what's going wrong in the download

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜