×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Regular Contributor
    Join Date
    Dec 2012
    Location
    Indonesia,DKI Jakarta
    Posts
    87

    Question Image.createImage(byte[] bytes, int offset, int len) does not works

    Hello All,

    I use create image method: createImage(byte[] bytes, int offset, int len). but that method does not works. here is my code:
    Code:
    import java.io.ByteArrayOutputStream;
    import java.io.InputStream;
    
    import javax.microedition.io.file.FileConnection;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Form;
    //import javax.microedition.lcdui.Image;
    import com.sun.lwuit.Image;
    import javax.microedition.midlet.MIDlet;
    import javax.microedition.midlet.MIDletStateChangeException;
    
    
    public class Midlet extends MIDlet {
    
    	TestForm form;
    	public byte[] imgBytes = null;
    	ByteArrayOutputStream baos = null;
    	InputStream in = null;
    	FileConnection file = null;
    	private Display display;
    	
    	public Midlet() {
    		System.out.println("Midlet running");
    	}
    
    	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
    		// TODO Auto-generated method stub
    
    	}
    
    	protected void pauseApp() {
    		// TODO Auto-generated method stub
    
    	}
    
    	protected void startApp() throws MIDletStateChangeException {
    //		Display.init(this);
    //		form = new TestForm();
    //		form.show();
    		
    		display = Display.getDisplay(this);
    		byte[] arrByte = loadFromApp();
    		System.out.println("byte array: "+arrByte.length);
    		System.out.println("imgBytes: "+arrByte);
    		in = this.getClass().getResourceAsStream("/logo.png");
    		Image imgLogo = Image.createImage(arrByte, (int)0, (int)arrByte.length);
    		System.out.println("imgLogo array: "+imgLogo);
    		
    //		Form A = new Form("ByteArray Image");
    //		A.append(imgLogo);
    //		display.setCurrent(A);
    	}
    
    	/**
    	 * Reads the local image resource and returns an array of bytes representing
    	 * the image
    	 * 
    	 * @param imgPath
    	 *            path to the image inside the app
    	 * @return array of bytes representing the image
    	 */
    	public byte[] loadFromApp()
    	{
    		StringBuffer sb = new StringBuffer();
    		
    		try {
    			baos = new ByteArrayOutputStream();
    			//this image is inside my mobile application
    			in = this.getClass().getResourceAsStream("/logo.png");
    			//byte[] buffer = new byte[1024];
    			int n = 0;
    			
    			while ((n = in.read()) != -1) {
    				//System.out.println("ayam");
    				sb.append((char)n);
    			}
    			
    			imgBytes = sb.toString().getBytes();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}finally{
    			//whatever happends close the streams
    			if (baos != null) {
    				try {
    					baos.close();
    				} catch (Exception e2) {
    					// TODO: handle exception
    				}
    			}
    			
    			if(in != null)
    			{
    				try {
    					in.close();
    				} catch (Exception e2) {
    					// TODO: handle exception
    				}
    			}
    		}
    		
    		return imgBytes;
    	}
    }
    My goal is to convert the image into bytearray and then create image from bytearray. But the above code produce NullPointerException when create image. Is this a bug from lwuit Image..??. Because i used Image class from lcdui, it's working perfectly.




    Best Regards,
    Iqbal

  2. #2
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,738

    Re: Image.createImage(byte[] bytes, int offset, int len) does not works

    Chances that it is the Image object which is null. String and StringBuffer are not good containers for storing bytes. First of all they contain 16-bit Unicode characters, so the memory allocation is doubled, and getting back the byte array may imply some side effects.
    Use ByteArrayOutputStream.
    I hope you are trying this for practicing only, createImage has variants both for accepting path or stream too.

  3. #3
    Regular Contributor
    Join Date
    Dec 2012
    Location
    Indonesia,DKI Jakarta
    Posts
    87

    Re: Image.createImage(byte[] bytes, int offset, int len) does not works

    wizard_hu, I already try with ByteArrayOutputStream and the result is same. NullPointerException. for detail the error:

    java.lang.NullPointerException
    at com.sun.lwuit.Image.createImage(+9)
    at Midlet.startApp(+78)
    at javax.microedition.midlet.MIDletProxy.startApp(+7)
    null
    null
    null
    java.lang.NullPointerException
    at com.sun.lwuit.Image.createImage(+9)
    at Midlet.startApp(+78)
    at javax.microedition.midlet.MIDletProxy.startApp(+7)
    null
    null
    null

    and here is the code that used ByteArrayOutputStream:

    Code:
    import java.io.ByteArrayOutputStream;
    import java.io.InputStream;
    
    import javax.microedition.io.file.FileConnection;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Form;
    //import javax.microedition.lcdui.Image;
    import com.sun.lwuit.Image;
    import javax.microedition.midlet.MIDlet;
    import javax.microedition.midlet.MIDletStateChangeException;
    
    
    public class Midlet extends MIDlet {
    
    	TestForm form;
    	public byte[] imgBytes = null;
    	ByteArrayOutputStream baos = null;
    	InputStream in = null;
    	FileConnection file = null;
    	private Display display;
    	
    	public Midlet() {
    		System.out.println("Midlet running");
    	}
    
    	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
    		// TODO Auto-generated method stub
    
    	}
    
    	protected void pauseApp() {
    		// TODO Auto-generated method stub
    
    	}
    
    	protected void startApp() throws MIDletStateChangeException {
    //		Display.init(this);
    //		form = new TestForm();
    //		form.show();
    		
    		display = Display.getDisplay(this);
    		byte[] arrByte = loadFromApp();
    		System.out.println("byte array: "+arrByte.length);
    		System.out.println("imgBytes: "+arrByte);
    		in = this.getClass().getResourceAsStream("/logo.png");
    		Image imgLogo = Image.createImage(arrByte, (int)0, (int)arrByte.length);
    		System.out.println("imgLogo array: "+imgLogo);
    		
    //		Form A = new Form("ByteArray Image");
    //		A.append(imgLogo);
    //		display.setCurrent(A);
    	}
    
    	/**
    	 * Reads the local image resource and returns an array of bytes representing
    	 * the image
    	 * 
    	 * @param imgPath
    	 *            path to the image inside the app
    	 * @return array of bytes representing the image
    	 */
    	public byte[] loadFromApp()
    	{
    		StringBuffer sb = new StringBuffer();
    		
    		try {
    			baos = new ByteArrayOutputStream();
    			//this image is inside my mobile application
    			in = this.getClass().getResourceAsStream("/logo.png");
    			byte[] buffer = new byte[1024];
    			int n = 0;
    			
    			while ((n = in.read(buffer)) != -1) {
    				//System.out.println("ayam");
    				//sb.append((char)n);
    				baos.write(buffer,0,n);
    			}
    			
    			imgBytes = baos.toByteArray();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}finally{
    			//whatever happends close the streams
    			if (baos != null) {
    				try {
    					baos.close();
    				} catch (Exception e2) {
    					// TODO: handle exception
    				}
    			}
    			
    			if(in != null)
    			{
    				try {
    					in.close();
    				} catch (Exception e2) {
    					// TODO: handle exception
    				}
    			}
    		}
    		
    		return imgBytes;
    	}
    }


    Best Regards,
    Iqbal

  4. #4
    Registered User
    Join Date
    Mar 2013
    Location
    United Kingdom
    Posts
    161

    Re: Image.createImage(byte[] bytes, int offset, int len) does not works

    Can you check if your function loadFromApp() actually returns a byte[]? My thought is that arrByte isn't getting initialized and hence you're getting that null pointer error.

  5. #5
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,738

    Re: Image.createImage(byte[] bytes, int offset, int len) does not works

    loadFromApp internally hides any possible exceptions, consider tracking/debugging what happens inside. createImage probably gets a null argument, which comes from an earlier exception.
    EDIT: What Grift points out, that should be checked too, returning member variables is a strange pattern in general.

  6. #6
    Registered User
    Join Date
    Feb 2004
    Posts
    1,980

    Re: Image.createImage(byte[] bytes, int offset, int len) does not works

    You had better be sure that the length of bytearray is not zero.
    This is the codes which can creat a image from web via bytearray.



    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){}



    Maybe help you.
    Last edited by r60600; 2013-04-19 at 05:14.

  7. #7
    Regular Contributor
    Join Date
    Dec 2012
    Location
    Indonesia,DKI Jakarta
    Posts
    87

    Re: Image.createImage(byte[] bytes, int offset, int len) does not works

    Quote Originally Posted by grift View Post
    Can you check if your function loadFromApp() actually returns a byte[]? My thought is that arrByte isn't getting initialized and hence you're getting that null pointer error.
    Grift, I have check my code for the variable arrByte. It has value from returning loadFromApp method. So the variable arrByte is not null

  8. #8
    Regular Contributor
    Join Date
    Dec 2012
    Location
    Indonesia,DKI Jakarta
    Posts
    87

    Re: Image.createImage(byte[] bytes, int offset, int len) does not works

    Quote Originally Posted by wizard_hu_ View Post
    loadFromApp internally hides any possible exceptions, consider tracking/debugging what happens inside. createImage probably gets a null argument, which comes from an earlier exception.
    EDIT: What Grift points out, that should be checked too, returning member variables is a strange pattern in general.
    wizard_hu_ there is no problem in loadFromApp() method. I have debug the code and the method return the byte array value. But still NullPointerException when i create Image with byte array.

  9. #9
    Regular Contributor
    Join Date
    Dec 2012
    Location
    Indonesia,DKI Jakarta
    Posts
    87

    Re: Image.createImage(byte[] bytes, int offset, int len) does not works

    I user Image.createImage(byte[] bytes, int offset, int len) method,It works. but with lwuit always NullPointerException Anyone can help...?
    Last edited by muhamad.iqbal; 2013-04-24 at 07:20.

  10. #10
    Registered User
    Join Date
    Mar 2013
    Location
    United Kingdom
    Posts
    161

    Re: Image.createImage(byte[] bytes, int offset, int len) does not works

    If someone doesn't beat me to it, I will try to demonstrate a working example later tonight.

  11. #11
    Regular Contributor
    Join Date
    Dec 2012
    Location
    Indonesia,DKI Jakarta
    Posts
    87

    Re: Image.createImage(byte[] bytes, int offset, int len) does not works

    Quote Originally Posted by grift View Post
    If someone doesn't beat me to it, I will try to demonstrate a working example later tonight.
    Ok grift, thank you so much. Please try my code. Because today i already try it, debug it again. Still same NullPointerException. Where as in debug tool, i got the value of arrByte as byte[]. Grift please, post your result here, if you success.

  12. #12
    Regular Contributor
    Join Date
    Dec 2012
    Location
    Indonesia,DKI Jakarta
    Posts
    87

    Re: Image.createImage(byte[] bytes, int offset, int len) does not works

    Quote Originally Posted by grift View Post
    If someone doesn't beat me to it, I will try to demonstrate a working example later tonight.
    Grift, Did you try my code..?. Somebody can help...?

  13. #13
    Registered User
    Join Date
    Mar 2013
    Location
    United Kingdom
    Posts
    161

    Re: Image.createImage(byte[] bytes, int offset, int len) does not works

    Sorry, muhamad, I didn't get a chance. I will definitely try tonight.

  14. #14
    Regular Contributor
    Join Date
    Dec 2012
    Location
    Indonesia,DKI Jakarta
    Posts
    87

    Question Re: Image.createImage(byte[] bytes, int offset, int len) does not works

    Quote Originally Posted by grift View Post
    Sorry, muhamad, I didn't get a chance. I will definitely try tonight.
    Ok grift, Did you get a chance and try it..? . Or somebody have chance to try my code..?



    Best Regards,
    Iqbal

  15. #15
    Registered User
    Join Date
    Mar 2013
    Location
    United Kingdom
    Posts
    161

    Re: Image.createImage(byte[] bytes, int offset, int len) does not works

    Had a look, and have had as little luck as you have had. Is there any reason why you can't just use LCDUI instead of LWUIT? If you must use LWUIT, can you not just create the image by passing down the path of the image instead?
    Code:
    Image imgLogo = Image.createImage("/logo.png");
    Having said that, there should be no reason why the code you have doesn't work, especially considering it works perfectly for LCDUI.

Similar Threads

  1. Converting an image into bytes and writing the bytes into a file in glassfish folders
    By powerohit@gmail.com in forum Mobile Java Networking & Messaging & Security
    Replies: 1
    Last Post: 2011-03-06, 11:11
  2. Image.createImage --> one picture works, the other one not?
    By stechbeitel in forum Mobile Java General
    Replies: 5
    Last Post: 2009-04-14, 07:13
  3. Replies: 6
    Last Post: 2008-09-25, 18:09
  4. createImage(byte[],int,int) bug on Nokia 7650
    By bachin in forum Mobile Java General
    Replies: 3
    Last Post: 2005-09-16, 03:49
  5. createImage(byte[] argbData,...)
    By DarthSteff in forum Mobile Java Tools & SDKs
    Replies: 0
    Last Post: 2005-08-19, 10:59

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×