J2ME: Display image from filesystem

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2009
Posts: 5
Reputation: Pmarcoen is an unknown quantity at this point 
Solved Threads: 0
Pmarcoen Pmarcoen is offline Offline
Newbie Poster

J2ME: Display image from filesystem

 
0
  #1
Aug 30th, 2009
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 ?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 121
Reputation: puneetkay is on a distinguished road 
Solved Threads: 23
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: J2ME: Display image from filesystem

 
0
  #2
Aug 31st, 2009
Originally Posted by Pmarcoen View 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 ?
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.
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer


Admin of Pikk - Object Relational Mapping Framework
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,207
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 486
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: J2ME: Display image from filesystem

 
-1
  #3
Aug 31st, 2009
@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:	1
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.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 121
Reputation: puneetkay is on a distinguished road 
Solved Threads: 23
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: J2ME: Display image from filesystem

 
0
  #4
Aug 31st, 2009
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,
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer


Admin of Pikk - Object Relational Mapping Framework
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 5
Reputation: Pmarcoen is an unknown quantity at this point 
Solved Threads: 0
Pmarcoen Pmarcoen is offline Offline
Newbie Poster

Re: J2ME: Display image from filesystem

 
0
  #5
Aug 31st, 2009
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 !
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,207
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 486
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: J2ME: Display image from filesystem

 
-1
  #6
Aug 31st, 2009
@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
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 5
Reputation: Pmarcoen is an unknown quantity at this point 
Solved Threads: 0
Pmarcoen Pmarcoen is offline Offline
Newbie Poster

Re: J2ME: Display image from filesystem

 
0
  #7
Aug 31st, 2009
Hmm .. okay,

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

  1. FileConnection fconn;
  2. try {
  3. fconn = (FileConnection)Connector.open("file:///C:/Data/Images/200908/25082009004.jpg");
  4. InputStream in = fconn.openInputStream();
  5. image = Image.createImage(in);
  6. form.append(image);
  7. fconn.close();
  8. } catch (IOException e1) {
  9. // TODO Auto-generated catch block
  10. e1.printStackTrace();
  11. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,207
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 486
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: J2ME: Display image from filesystem

 
-1
  #8
Aug 31st, 2009
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.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 5
Reputation: Pmarcoen is an unknown quantity at this point 
Solved Threads: 0
Pmarcoen Pmarcoen is offline Offline
Newbie Poster

Re: J2ME: Display image from filesystem

 
0
  #9
Aug 31st, 2009
Thanks for staying with me on this one Peter, took your remarks in, still getting unhandled exception though ..

Code :

  1. form = new Form("HelloMobile");
  2.  
  3. FileConnection fconn;
  4. TextField textAfter = null;
  5. try {
  6. fconn = (FileConnection)Connector.open("file:///C:/Data/Images/200908/200908A0/25082009004.jpg");
  7. InputStream in = fconn.openInputStream();
  8. image = Image.createImage(in);
  9. in.close();
  10. fconn.close();
  11. } catch (IOException e1) {
  12. textAfter = new TextField("e1: " + e1.getMessage(),"",30,TextField.ANY);
  13. }
  14. catch (IllegalModeException e2) {
  15. textAfter = new TextField("e2: " + e2.getMessage(),"",30,TextField.ANY);
  16. }
  17. catch (java.lang.SecurityException e3) {
  18. textAfter = new TextField("e3: " + e3.getMessage(),"",30,TextField.ANY);
  19. }
  20.  
  21. if(image != null){
  22. form.append(image);
  23. }
  24. else {
  25. textAfter = new TextField("empty","",30,TextField.ANY);
  26. }
  27.  
  28. if(textAfter != null){
  29. form.append(textAfter);
  30. }
  31.  
  32. display = Display.getDisplay(this);
  33. display.setCurrent(form);
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,207
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 486
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: J2ME: Display image from filesystem

 
-1
  #10
Aug 31st, 2009
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:	0
Size:	81.8 KB
ID:	11412
  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. }

  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. }
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC