944,008 Members | Top Members by Rank

You are currently viewing page 1 of this multi-page discussion thread
Aug 30th, 2009
0

J2ME: Display image from filesystem

Expand Post »
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 ?
Last edited by peter_budo; Mar 8th, 2010 at 3:04 am. Reason: Adding code tags to old thread
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Pmarcoen is offline Offline
5 posts
since Aug 2009
Aug 31st, 2009
0

Re: J2ME: Display image from filesystem

Click to Expand / Collapse  Quote originally posted by Pmarcoen ...
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,
Last edited by puneetkay; Aug 31st, 2009 at 3:13 am.
Reputation Points: 51
Solved Threads: 24
Junior Poster
puneetkay is offline Offline
122 posts
since Nov 2007
Aug 31st, 2009
-1

Re: J2ME: Display image from filesystem

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

Name:	root_view.png
Views:	1531
Size:	2.8 KB
ID:	11408

    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)
Last edited by peter_budo; Aug 31st, 2009 at 4:24 am.
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,658 posts
since Dec 2004
Aug 31st, 2009
0

Re: J2ME: Display image from filesystem

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,
Reputation Points: 51
Solved Threads: 24
Junior Poster
puneetkay is offline Offline
122 posts
since Nov 2007
Aug 31st, 2009
0

Re: J2ME: Display image from filesystem

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...pi-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 !
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Pmarcoen is offline Offline
5 posts
since Aug 2009
Aug 31st, 2009
-1

Re: J2ME: Display image from filesystem

@puneetkay LOL seems you are offended even though the above line came from movie , 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
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,658 posts
since Dec 2004
Aug 31st, 2009
0

Re: J2ME: Display image from filesystem

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();
		}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Pmarcoen is offline Offline
5 posts
since Aug 2009
Aug 31st, 2009
-1

Re: J2ME: Display image from filesystem

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
Last edited by peter_budo; Aug 31st, 2009 at 8:34 am.
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,658 posts
since Dec 2004
Aug 31st, 2009
0

Re: J2ME: Display image from filesystem

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);
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Pmarcoen is offline Offline
5 posts
since Aug 2009
Aug 31st, 2009
-1

Re: J2ME: Display image from filesystem

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
Click image for larger version

Name:	emulator.png
Views:	57
Size:	81.8 KB
ID:	11412

Java Syntax (Toggle Plain Text)
  1. import javax.microedition.midlet.MIDlet;
  2. import javax.microedition.lcdui.Display;
  3.  
  4. public class LocalImage extends MIDlet{
  5.  
  6. private Display display;
  7.  
  8. public LocalImage(){}
  9.  
  10. public void startApp(){
  11. if(display == null){
  12. display = Display.getDisplay(this);
  13. }
  14. display.setCurrent(new MyForm());
  15. }
  16.  
  17. public void pauseApp(){}
  18.  
  19. public void destroyApp(boolean unconditionl){
  20.  
  21. }
  22. }

Java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2. import javax.microedition.io.*;
  3. import javax.microedition.io.file.IllegalModeException;
  4. import javax.microedition.lcdui.*;
  5. import javax.microedition.io.file.FileConnection;
  6.  
  7. public class MyForm extends Form {
  8.  
  9. private Image image;
  10.  
  11. public MyForm() {
  12. super("Display Image");
  13.  
  14. FileConnection fconn;
  15. TextField textAfter = null;
  16. try {
  17. fconn = (FileConnection) Connector.open("file:///c:/pictures/camera_semc/hdd.png");///Data/Images/200908/200908A0/25082009004.jpg");
  18.  
  19.  
  20. InputStream in = fconn.openInputStream();
  21. image = Image.createImage(in);
  22. in.close();
  23. fconn.close();
  24. }
  25. catch (IOException e1) {
  26. textAfter = new TextField("e1: " + e1.getMessage(), "", 30, TextField.ANY);
  27. }
  28. catch (IllegalModeException e2) {
  29. textAfter = new TextField("e2: " + e2.getMessage(), "", 30, TextField.ANY);
  30. }
  31. catch (java.lang.SecurityException e3) {
  32. textAfter = new TextField("e3: " + e3.getMessage(), "", 30, TextField.ANY);
  33. }
  34.  
  35. if (image != null) {
  36. append(image);
  37. }
  38. else {
  39. textAfter = new TextField("empty", "", 30, TextField.ANY);
  40. }
  41.  
  42. if (textAfter != null) {
  43. append(textAfter);
  44. }
  45. }
  46. }
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,658 posts
since Dec 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Mobile Development Forum Timeline: J2ME: how to select multiple contacts from phonebook
Next Thread in Mobile Development Forum Timeline: Midlet Static Variable problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC