1

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) != ".")
            {
                print("<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 ?

  • I typed in the address bar of my browser this : http://192.168.1.123/imfmobile/photoj2meupload/downloadphoto.php?dossierClient=RahajarsonMarvin , and I got a correct webpage which displays a photo. I right-clicked the page and I got its source code and it gave : –  Sep 08 '11 at 09:39
  • I think you just answered your own question. Instead of an image, downloadphoto.php is giving you html that includes an tag and the real path to the image. – AndrewR Sep 08 '11 at 09:42

1 Answers1

0

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

gnat
  • 6,213
  • 108
  • 53
  • 73
  • I debugged it with the emulator and it is the Image.createImage which caused the problem. –  Sep 08 '11 at 10:53
  • @AndyFrédéric did you figure what kind of problem was in `Image.createImage`? Exception? or null `rawImg`? or something in length parameter? something else? – gnat Sep 08 '11 at 11:52
  • 1
    I found the solution !!! My error was in the setting of the url value : I googled about "j2me download photo" and I found a tutorial which shows the way to set the url. It is like this http://www.sitename.com/folder/imagename.png ; so the way I set my url in my code was wrong. And now it works fine after setting the right url !!! Thanks –  Sep 08 '11 at 12:59