×
Namespaces

Variants
Actions
(Difference between revisions)

Get Web Image in MIDlet

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot addition of Template:ArticleMetaData)
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot update - Fix metadata)
(3 intermediate revisions by one user not shown)
Line 1: Line 1:
{{ArticleMetaData
+
[[Category:Java ME]][[Category:Series 40]][[Category:Networking]]
 +
{{ArticleMetaData <!-- v1.2 -->
 
|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]] -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
Line 6: Line 7:
 
|platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later -->
 
|platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later -->
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->  
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
+
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
|capabilities=<!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
+
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 
|keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
 
|keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
|id= <!-- Article Id (Knowledge base articles only) -->
+
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
|language=<!-- Language category code for non-English topics - e.g. Lang-Chinese -->
+
|translated-by= <!-- [[User:XXXX]] -->
|review-by=<!-- After re-review: [[User:username]] -->
+
|translated-from-title= <!-- Title only -->
|review-timestamp=<!-- After re-review: YYYYMMDD -->
+
|translated-from-id= <!-- Id of translated revision -->
|update-by=<!-- After significant update: [[User:username]]-->
+
|review-by= <!-- After re-review: [[User:username]] -->
|update-timestamp=<!-- After significant update: YYYYMMDD -->
+
|review-timestamp= <!-- After re-review: YYYYMMDD -->
|creationdate=20110602
+
|update-by= <!-- After significant update: [[User:username]]-->
|author=[[User:R60600]]
+
|update-timestamp= <!-- After significant update: YYYYMMDD -->
}}[[Category:Java ME]][[Category:Series 40]]
+
|creationdate= 20110602
 +
|author= [[User:R60600]]
 +
}}
  
 
MIDlet usually uses web image which can be get via
 
MIDlet usually uses web image which can be get via
<pre>private String url;
+
<code java>private String url;
 
private byte[] data;
 
private byte[] data;
 
private int datalength;  
 
private int datalength;  
Line 35: Line 38:
 
if (datalength &gt; 0)  
 
if (datalength &gt; 0)  
 
image = Image.createImage(data, 0, datalength);  
 
image = Image.createImage(data, 0, datalength);  
}catch (java.io.IOException x){}</pre>
+
}catch (java.io.IOException x){}
 +
</code>
 
If the image object is created successfully,it can be displayed by usual way.
 
If the image object is created successfully,it can be displayed by usual way.
 
Such like in Canvas
 
Such like in Canvas
<pre>protected void paint(Graphics g)
+
<code java>protected void paint(Graphics g)
 
{
 
{
 
g.drawImage(image,0, 0, Graphics.LEFT|Graphics.TOP);
 
g.drawImage(image,0, 0, Graphics.LEFT|Graphics.TOP);
}</pre>
+
}</code>
 +
<!-- Translation --> [[zh-hans:网络图片资源处理]]

Revision as of 09:24, 26 September 2012

Article Metadata
Article
Created: r60600 (02 Jun 2011)
Last edited: hamishwillee (26 Sep 2012)

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 &gt; 0)
image = Image.createImage(data, 0, datalength);
}catch (java.io.IOException x){}

If the image object is created successfully,it can be displayed by usual way. Such like in Canvas

protected void paint(Graphics g)
{
g.drawImage(image,0, 0, Graphics.LEFT|Graphics.TOP);
}
10 page views in the last 30 days.
×