×
Namespaces

Variants
Actions
(Difference between revisions)

Converting a resource into a byte array in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
lpvalente (Talk | contribs)
m (Lpvalente -)
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Java ME]][[Category:MIDP 2.0]][[Category:Code Examples]][[Category:WMA 2.0 (JSR-205)]][[Category:S60 3rd Edition FP1]][[Category:Code Snippet]]
+
[[Category:Java ME]][[Category:MIDP 2.0]][[Category:Code Snippet]][[Category:WMA 2.0 (JSR-205)]][[Category:S60 3rd Edition FP1]][[Category:Code Snippet]][[Category:Symbian]][[Category:Series 40]][[Category:Files/Data]][[Category:S60 2nd Edition FP2]][[Category:S60 2nd Edition FP3]][[Category:S60 3rd Edition (initial release)]][[Category:S60 3rd Edition FP1]][[Category:S60 3rd Edition FP2]][[Category:S60 5th Edition]][[Category:Symbian^3]][[Category:Symbian Anna]][[Category:Nokia Belle]][[Category:Series 40 3rd Edition (initial release)]][[Category:Series 40 3rd Edition FP1]][[Category:Series 40 3rd Edition FP2]][[Category:Series 40 5th Edition (initial release)]][[Category:Series 40 5th Edition FP1]][[Category:Series 40 6th Edition (initial release)]][[Category:Series 40 6th Edition FP1]][[Category:Series 40 Developer Platform 1.0]][[Category:Series 40 Developer Platform 1.1]][[Category:Series 40 Developer Platform 2.0]]
__NOTOC__
+
{{VersionHint}}
__NOEDITSECTION__
+
{{ArticleMetaData <!-- v1.2 -->
{{KBCS}}
+
{{ArticleMetaData
+
|id=CS000979
+
|platform=S60 3rd Edition, FP1
+
|devices=Nokia N95 8GB
+
|category=Java ME
+
|subcategory=WMA 2.0 (JSR-205)
+
|creationdate=May 27, 2008
+
|keywords=java.lang.Class, java.io.InputStream, java.io.ByteArrayOutputStream, java.io.IOException, java.lang.Class.getResourceAsStream(), java.io.InputStream.read(), java.io.ByteArrayOutputStream.write(), java.io.ByteArrayOutputStream.toByteArray(), java.io.InputStream.close(), java.io.ByteArrayOutputStream.close()
+
 
+
 
|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]]) -->
|sdk=[http://www.developer.nokia.com/Develop/Java/ Nokia SDK 1.0 for Java]
+
|devices= Nokia N95 8GB
|devicecompatability=<!-- Compatible devices (e.g.: All* (must have GPS) ) -->
+
|sdk= [http://www.developer.nokia.com/Develop/Java/ Nokia SDK 1.0 for Java]
|signing=<!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
+
|platform= S60 3rd Edition, FP1
|capabilities=<!-- Capabilities required (e.g. Location, NetworkServices. -->)
+
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
|update-by=[[User:tiviinik]]
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
|update-timestamp=20120110
+
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
|review-by=<!-- [[User:XXXX]]-->
+
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
|review-timestamp=<!-- 20110105-->
+
|keywords= java.lang.Class, java.io.InputStream, java.io.ByteArrayOutputStream, java.io.IOException, java.lang.Class.getResourceAsStream(), java.io.InputStream.read(), java.io.ByteArrayOutputStream.write(), java.io.ByteArrayOutputStream.toByteArray(), java.io.InputStream.close(), java.io.ByteArrayOutputStream.close()
|author=[[User:Tapla]]
+
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- [[User:XXXX]]-->
 +
|review-timestamp= <!-- 20110105-->
 +
|update-by= [[User:tiviinik]]
 +
|update-timestamp= 20120110
 +
|creationdate= 20080521
 +
|author= [[User:Tapla]]
 +
<!-- The following are not in current metadata -->
 +
|subcategory= WMA 2.0 (JSR-205)
 +
|id= CS000979
 
}}
 
}}
{{Archived|timestamp=20120223102402|user=roy.debjit| }}
+
 
 
==Overview==
 
==Overview==
 
+
{{Abstract|This code snippet demonstrates how to convert a resource into a byte array. The snippet assumes that the resource is included in the [[JAR]] file, which is used to distribute the application.}}
This code snippet demonstrates how to convert a resource into a byte array. The snippet assumes that the resource is included in the [[JAR]] file, which is used to distribute the application.
+
  
 
'''Example''': To include a file to the JAR package in [http://netbeans.org/ NetBeans 6.1], do as follows:
 
'''Example''': To include a file to the JAR package in [http://netbeans.org/ NetBeans 6.1], do as follows:
Line 33: Line 34:
 
# In the '''Projects''' window (select '''Window''' &gt; '''Projects''' if it's not visible), open the project node for the project to which you want to add the file.
 
# In the '''Projects''' window (select '''Window''' &gt; '''Projects''' if it's not visible), open the project node for the project to which you want to add the file.
 
# Right-click on the '''Resources''' node under the project and choose '''Add Folder'''. The Add Folder dialog box opens.
 
# Right-click on the '''Resources''' node under the project and choose '''Add Folder'''. The Add Folder dialog box opens.
# Select the folder where the resource is (for example, <tt>dist/images</tt>).
+
# Select the folder where the resource is (for example, {{Icode|dist/images}}).
 
# Click '''Open'''. The folder is added to the project.
 
# Click '''Open'''. The folder is added to the project.
 
# Clean and build the project (right-click on the project node and select '''Clean &amp; Build''').
 
# Clean and build the project (right-click on the project node and select '''Clean &amp; Build''').
Line 44: Line 45:
  
 
==Source==
 
==Source==
 
 
<code java>
 
<code java>
 
import java.io.ByteArrayOutputStream;
 
import java.io.ByteArrayOutputStream;
Line 80: Line 80:
 
</code>
 
</code>
  
The method above can be used, for example, with multipart messages. In the following, a <tt>MessagePart</tt> object is constructed from a byte array which represents raw data from a JPEG image:
+
The method above can be used, for example, with multipart messages. In the following, a {{Icode|MessagePart}} object is constructed from a byte array which represents raw data from a JPEG image:
  
 
<code java>
 
<code java>
Line 116: Line 116:
 
==See also==
 
==See also==
  
* [[CS000978 - Sending a multipart MMS]]
+
* [[Sending a multipart MMS using Java ME]]

Revision as of 21:35, 7 September 2012

Contents

Version Hint

Windows Phone: [[Category:Windows Phone]]
[[Category:Windows Phone 7.5]]
[[Category:Windows Phone 8]]

Nokia Asha: [[Category:Nokia Asha]]
[[Category:Nokia Asha Platform 1.0]]

Series 40: [[Category:Series 40]]
[[Category:Series 40 1st Edition]] [[Category:Series 40 2nd Edition]]
[[Category:Series 40 3rd Edition (initial release)]] [[Category:Series 40 3rd Edition FP1]] [[Category:Series 40 3rd Edition FP2]]
[[Category:Series 40 5th Edition (initial release)]] [[Category:Series 40 5th Edition FP1]]
[[Category:Series 40 6th Edition (initial release)]] [[Category:Series 40 6th Edition FP1]] [[Category:Series 40 Developer Platform 1.0]] [[Category:Series 40 Developer Platform 1.1]] [[Category:Series 40 Developer Platform 2.0]]

Symbian: [[Category:Symbian]]
[[Category:S60 1st Edition]] [[Category:S60 2nd Edition (initial release)]] [[Category:S60 2nd Edition FP1]] [[Category:S60 2nd Edition FP2]] [[Category:S60 2nd Edition FP3]]
[[Category:S60 3rd Edition (initial release)]] [[Category:S60 3rd Edition FP1]] [[Category:S60 3rd Edition FP2]]
[[Category:S60 5th Edition]]
[[Category:Symbian^3]] [[Category:Symbian Anna]] [[Category:Nokia Belle]]

Article Metadata
Tested with
Devices(s): Nokia N95 8GB
CompatibilityArticle
Keywords: java.lang.Class, java.io.InputStream, java.io.ByteArrayOutputStream, java.io.IOException, java.lang.Class.getResourceAsStream(), java.io.InputStream.read(), java.io.ByteArrayOutputStream.write(), java.io.ByteArrayOutputStream.toByteArray(), java.io.InputStream.close(), java.io.ByteArrayOutputStream.close()
Created: tapla (21 May 2008)
Updated: tiviinik (10 Jan 2012)
Last edited: lpvalente (07 Sep 2012)

Overview

This code snippet demonstrates how to convert a resource into a byte array. The snippet assumes that the resource is included in the JAR file, which is used to distribute the application.

Example: To include a file to the JAR package in NetBeans 6.1, do as follows:

  1. In the Projects window (select Window > Projects if it's not visible), open the project node for the project to which you want to add the file.
  2. Right-click on the Resources node under the project and choose Add Folder. The Add Folder dialog box opens.
  3. Select the folder where the resource is (for example, dist/images).
  4. Click Open. The folder is added to the project.
  5. Clean and build the project (right-click on the project node and select Clean & Build).
  6. The resource can be obtained in the code, for example, like this:
InputStream inputStream = getClass().getResourceAsStream("image.jpg");
// ...

Source

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* Constructs a byte array and fills it with data that is read from the
* specified resource.
* @param filename the path to the resource
* @return the specified resource as a byte array
* @throws java.io.IOException if the resource cannot be read, or the
* bytes cannot be written, or the streams cannot be closed
*/

private byte[] obtainByteData(String filename) throws IOException {
InputStream inputStream = getClass().getResourceAsStream(filename);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
 
// Read bytes from the input stream in bytes.length-sized chunks and write
// them into the output stream
for (int readBytes = inputStream.read(); readBytes >= 0; readBytes = inputStream.read())
outputStream.write(readBytes);
 
// Convert the contents of the output stream into a byte array
byte[] byteData = outputStream.toByteArray();
 
// Close the streams
inputStream.close();
outputStream.close();
 
return byteData;
}

The method above can be used, for example, with multipart messages. In the following, a MessagePart object is constructed from a byte array which represents raw data from a JPEG image:

import javax.wireless.messaging.MessagePart;
import javax.wireless.messaging.SizeExceededException;
 
// ...
 
/**
* Constructs a MessagePart which can be added to a MultipartMessage.
* @return the constructed MessagePart
* @throws javax.wireless.messaging.SizeExceededException if the contents
* is larger than the available memory or supported size for the message
* part
* @throws java.io.IOException if the byte data cannot be obtained
*/

private MessagePart createMsgPart() throws SizeExceededException,
IOException {
String imageContentID = "image01";
String imageContentLocation = "image.jpg";
String jpgMIME = "image/jpeg";
// Convert the image into a byte array and construct a message part using
// it
byte[] imageContent = obtainByteData(imageContentLocation);
MessagePart messagePart = new MessagePart(imageContent, jpgMIME,
imageContentID, imageContentLocation, null);
return messagePart;
}

Postconditions

A specified resource is converted into a byte array.

See also

240 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×