I seem to have a problem displaying an image from the phone's filesystem in J2ME.

The following code works for images that are in the res folder but not if the path references to a file on the filesystem ..

path = "file:///C:/Data/Images/200908/25082009004.jpg";
try {
image = Image.createImage(path);
} catch (IOException e) {
e.printStackTrace();
}
imageItem = new ImageItem(null,image,ImageItem.LAYOUT_CENTER,"Imag e not found");
form.append(imageItem);

Do I need to use an inputStream ?
If so, how ?

Recommended Answers

All 11 Replies

I seem to have a problem displaying an image from the phone's filesystem in J2ME.

The following code works for images that are in the res folder but not if the path references to a file on the filesystem ..

path = "file:///C:/Data/Images/200908/25082009004.jpg";
try {
image = Image.createImage(path);
} catch (IOException e) {
e.printStackTrace();
}
imageItem = new ImageItem(null,image,ImageItem.LAYOUT_CENTER,"Imag e not found");
form.append(imageItem);

Do I need to use an inputStream ?
If so, how ?

Hello,

I think the problem is with Image path. loading image from drive isn't supported by J2ME (because theres no drives in mobile) or the image path is incorrect.

Also, theres no need to create ImageItem object, you can append Image directly.

Demo Code

Regards,

@puneetkay in traditional street proverb "I gona nail your a$$"linked website is worst case scenario of any tutorials
there is support for loading images from device in-build memory and memory card

All you need to do is use JSR 75 - PDA Optional Packages for the J2ME™ Platform with File Connection API and you there. Here is simple example of device browser that can be used either to extend functionality in order to find and use selected file or simple reuse the principles used in this case (provide path, check file existence and use the file)

Firstly,

@peter_budo, Mind your language! This isnt your home street, Where you can use any cheap words.

If you read my reply with open eyes ...

"Hello,

I think the problem is... "

That means, Even im not sure about it and Im trying to help him according to my knowledge. If im wrong, then please correct even ill be glad if you help me and guide me to understand something new.

But you are just NOT allowed to say anything bad or abusive here. You are moderator here, So please act like a moderator and a teacher and seniors for others.

And about the demo (scenario), Im trying to help him according to his scenario and the logic he has made. Iknow its a bad code. But the thing is, A programmer whos unable to understand how to use Image and ImageItem, How can you be sure that he will be able to understand the help you provided him ? If hes not sure what hes doing in basic part and not able to recognise the problem, He will be able to understand the advance of it ?

Regards,

Yeah, I actually do understand Peter Budo.
I just ripped the image display code from a bad source apparently (probably roseindia.net).

I actually AM using the FileConnection API (code from http://j2mesamples.blogspot.com/2009/02/file-connection-using-j2me-api-jsr-75.html)

The thing is, if I'm in the else of the displayCurrentRoot function, that checkes whether the element of the file list is a folder or an item (jpg), I want it to display my image.

Normally it would just display the item's name with the currentDir variable like so :

append(currentDir,null);

Now I tried :

Image img = Image.createImage(currentDir);
append(currentDir,img);

which didn't work, so I tried the entire path :

String path = currentRoot.getURL() + currentDir;
Image img = Image.createImage(path);
append(currentDir,img);

Your help is appreciated !

@puneetkay LOL seems you are offended even though the above line came from movie :D, sorry for misunderstanding. Nevertheless if you try to help make sure you do not pick up first resource that pop up on google, which in these cases unfortunately is that terrible site.

@Pmarcoen as you can see the example that you found show only way to access folders (first comment to article) and few replies down the line it does show how you can get document. General idea is:

  • Check if FileConnection API is present on device (there is still number of device they do not include this library)
  • Provide file location/URL (this step can be replaced by example from my link where you browse local storage to find desired document)
  • Check if file or location exists (if location is hard coded)
  • Check for access permission (dependent on what ever you want to do with document)
  • Use either of the stream methods to read the document ( openDataInputStream() or openInputStream() )
  • and in your case use one of the createImage() methods create image

Hmm .. okay,

I'm still getting unhandled exception with this code, probably still overlooking something .. :

FileConnection fconn;
		try {
			fconn = (FileConnection)Connector.open("file:///C:/Data/Images/200908/25082009004.jpg");
			InputStream in = fconn.openInputStream();
			image = Image.createImage(in);
			form.append(image);
			fconn.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

openInputStream can beside IOException throw also IllegalModeException and java.lang.SecurityException you need to catch them too.

Few suggestions on top of this, you should also close InputStream, move form(append) out of try-catch inside simple if/else statement to check if image is not null and based on outcome of this boolean operation you will either add image to form or do nothing

Thanks for staying with me on this one Peter, took your remarks in, still getting unhandled exception though ..

Code :

form = new Form("HelloMobile");
		
		FileConnection fconn;
		TextField textAfter = null;
		try {
			fconn = (FileConnection)Connector.open("file:///C:/Data/Images/200908/200908A0/25082009004.jpg");
			InputStream in = fconn.openInputStream();
			image = Image.createImage(in);
			in.close();
			fconn.close();
		} catch (IOException e1) {
			textAfter = new TextField("e1: " + e1.getMessage(),"",30,TextField.ANY);
		}
		catch (IllegalModeException e2) {
			textAfter = new TextField("e2: " + e2.getMessage(),"",30,TextField.ANY);
		}
		catch (java.lang.SecurityException e3) {
			textAfter = new TextField("e3: " + e3.getMessage(),"",30,TextField.ANY);
		}
		
		if(image != null){
			form.append(image);
		}
		else {
			textAfter = new TextField("empty","",30,TextField.ANY);
		}
		
		if(textAfter != null){
			form.append(textAfter);
		}
        
        display = Display.getDisplay(this);        
        display.setCurrent(form);

Sorry I just spotted you are using wrong file location. You cannot provide file location as direct access to you pc system. To get this running from emulator you need to place your image into INSTALATION_WTK_DIRECTORY\appdb\DefaultColorPhone(or what ever phone you choose to use)\filesystem. Here you should find 3 drives c:/, e:/ and root:/ (or c%3A, e%3A and root1)and place image in one of them (c:/ is recommended as that stands for device memory also this image placement can be little try-and-error as I'm using Sony Ericsson WTK where image location is as C:\SonyEricsson\JavaME_SDK_CLDC\WTK2\appdb\SonyEricsson_W810_Emu\filesystem\c%3A\pictures\camera_semc\hdd.png and in application I used fconn = (FileConnection) Connector.open("file:///c:/pictures/camera_semc/hdd.png"); ). Bellow code is working fine on my pc with Sony Ericsson emulator import javax.microedition.midlet.MIDlet; import javax.microedition.lcdui.Display; public class LocalImage extends MIDlet{ private Display display; public LocalImage(){} public void startApp(){ if(display == null){ display = Display.getDisplay(this); } display.setCurrent(new MyForm()); } public void pauseApp(){} public void destroyApp(boolean unconditionl){ } }

import java.io.*;
import javax.microedition.io.*;
import javax.microedition.io.file.IllegalModeException;
import javax.microedition.lcdui.*;
import javax.microedition.io.file.FileConnection;

public class MyForm extends Form {

    private Image image;

    public MyForm() {
        super("Display Image");

        FileConnection fconn;
        TextField textAfter = null;
        try {
            fconn = (FileConnection) Connector.open("file:///c:/pictures/camera_semc/hdd.png");///Data/Images/200908/200908A0/25082009004.jpg");


            InputStream in = fconn.openInputStream();
            image = Image.createImage(in);
            in.close();
            fconn.close();
        }
        catch (IOException e1) {
            textAfter = new TextField("e1: " + e1.getMessage(), "", 30, TextField.ANY);
        }
        catch (IllegalModeException e2) {
            textAfter = new TextField("e2: " + e2.getMessage(), "", 30, TextField.ANY);
        }
        catch (java.lang.SecurityException e3) {
            textAfter = new TextField("e3: " + e3.getMessage(), "", 30, TextField.ANY);
        }                 

        if (image != null) {
            append(image);
        }
        else {
            textAfter = new TextField("empty", "", 30, TextField.ANY);
        }

        if (textAfter != null) {
            append(textAfter);
        }
    }
}

I'm testing directly on my phone (Nokia N95) and it's still returning unhandled exception, even with your code ..

Any other ideas ?

Thanks already mate !

Please keep in mind that hard-coded location of image is absolute to the emulator. You need to adjust it in regards of your phone. Linked example of device browser will help you get correct path with system call if you deploy them....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.