Get Web Image in MIDlet
hamishwillee
(Talk | contribs) m (Hamishwillee - Bot addition of Template:ArticleMetaData) |
hamishwillee
(Talk | contribs) m (Hamishwillee - Fix code blocks) |
||
| Line 1: | Line 1: | ||
| + | [[Category:Java ME]][[Category:Series 40]][[Category:Networking]] | ||
{{ArticleMetaData | {{ArticleMetaData | ||
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] --> | |sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] --> | ||
| Line 18: | Line 19: | ||
|creationdate=20110602 | |creationdate=20110602 | ||
|author=[[User:R60600]] | |author=[[User:R60600]] | ||
| − | }} | + | }} |
MIDlet usually uses web image which can be get via | MIDlet usually uses web image which can be get via | ||
| − | < | + | <code java>private String url; |
private byte[] data; | private byte[] data; | ||
private int datalength; | private int datalength; | ||
| Line 41: | Line 42: | ||
{ | { | ||
g.drawImage(image,0, 0, Graphics.LEFT|Graphics.TOP); | g.drawImage(image,0, 0, Graphics.LEFT|Graphics.TOP); | ||
| − | }</ | + | }</code> |
Revision as of 09:05, 12 December 2011
Article Metadata
MIDlet usually uses web image which can be get via
private String url;
private byte[] data;
private int datalength;
private DataInputStream dis;
private HttpConnection hc;
url = "http://xx/xx.gif" /* or "http://xx/xx.jpg" */
try {
hc = (HttpConnection)Connector.open(url);
datalength = (int)hc.getLength();
dis = hc.openDataInputStream();
data = new byte[datalength];
dis.readFully(data);
if (datalength > 0)
image = Image.createImage(data, 0, datalength);
}catch (java.io.IOException x){}</pre>
If the image object is created successfully,it can be displayed by usual way.
Such like in Canvas
<pre>protected void paint(Graphics g)
{
g.drawImage(image,0, 0, Graphics.LEFT|Graphics.TOP);
}

