| | |
J2ME: Display image from filesystem
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2009
Posts: 5
Reputation:
Solved Threads: 0
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 ?
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 ?
•
•
•
•
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 ?
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
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - Object Relational Mapping Framework
@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)
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
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,
@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
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - Object Relational Mapping Framework
•
•
Join Date: Aug 2009
Posts: 5
Reputation:
Solved Threads: 0
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 !
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 !
@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:
, 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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Aug 2009
Posts: 5
Reputation:
Solved Threads: 0
Hmm .. okay,
I'm still getting unhandled exception with this code, probably still overlooking something .. :
I'm still getting unhandled exception with this code, probably still overlooking something .. :
Java Syntax (Toggle Plain Text)
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
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Aug 2009
Posts: 5
Reputation:
Solved Threads: 0
Thanks for staying with me on this one Peter, took your remarks in, still getting unhandled exception though ..
Code :
Code :
Java Syntax (Toggle Plain Text)
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 Java Syntax (Toggle Plain Text)
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){ } }
Java Syntax (Toggle Plain Text)
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); } } }
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
![]() |
Similar Threads
- Trying to display an image rom mysql: whats wrong (PHP)
- how to display image in wml page using php (PHP)
- display image from databse (MySQL)
- how we display image on button? (VB.NET)
- how to display one image per day from an array? (JSP)
- display image when button clicked (Java)
- Display image from a database get me ascii codes (ASP)
- Display image for background (Java)
Other Threads in the Java Forum
- Previous Thread: Drawing Multiple line
- Next Thread: java master
| Thread Tools | Search this Thread |
actuate android api applet application applications array arrays automation balls bank binary bluetooth business chat class classes clear client code codesnippet collections component database defaultmethod development dice dragging ebook eclipse error event exception formatingtextintooltipjava fractal froglogic game givemetehcodez graphics gui hql html ide image infinite input integer intersect invokingapacheantprogrammatically j2me java javaprojects jni jpanel julia linux list loop looping map method methods mobile mysql netbeans newbie numbers openjavafx parameter php print problem program programming project recursion repositories scanner screen scrollbar server set size sms sort sorting sql sqlserver state storm string sun superclass swing swt text-file threads time tree windows






