Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
Expertise Level:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)

This page was last modified 23:05, 14 March 2008.

How to read an image from Gallery in Java ME

From Forum Nokia Wiki

This article shows, how to read an image from Gallery by using FileConnection API (JSR-75) and show it on Form. The Gallery folders can be accessed by using system properties, for example: System.getProperty("fileconn.dir.photos"). File contents are read by using InputStream, data is saved to a byte array and Image object is created by using Image.createImage() method.


The full source code for a test MIDlet:

ReadMIDlet.java

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.file.*;
import javax.microedition.io.*;
import java.io.*;
 
public class ReadMIDlet extends MIDlet implements CommandListener {
    private Form form;
    private String photos = "fileconn.dir.photos";
    private Image image;
    private Command exitCommand;
    
    public void startApp() {
        form = new Form("ReadMIDlet");
        exitCommand = new Command("Exit", Command.EXIT, 1);
        form.addCommand(exitCommand);
        form.setCommandListener(this);
        Display.getDisplay(this).setCurrent(form);
        String path = System.getProperty(photos);
        form.append(readFile(path + "image.png"));
    }
 
    public void pauseApp() {
    }
 
    public void destroyApp(boolean unconditional) {
    }
    
    public Image readFile(String path) {
        try {
            FileConnection fc = (FileConnection)Connector.open(path, Connector.READ);
            if(!fc.exists()) {
                System.out.println("File doesn't exist!");
            }
            else {
                int size = (int)fc.fileSize();
                InputStream is = fc.openInputStream();
                byte bytes[] = new byte[size];
                int length = is.read(bytes, 0, size);
                image = Image.createImage(bytes, 0, size);            	
            }            
            
        } catch (IOException ioe) {
            System.out.println("IOException: "+ioe.getMessage());            
        } catch (IllegalArgumentException iae) {
            System.out.println("IllegalArgumentException: "+iae.getMessage());            
        }
        return image;
   } 
    
    public void commandAction(Command c, Displayable d) {
        if (c == exitCommand) this.notifyDestroyed();
    }    
}
Related Discussions
Thread Thread Starter Forum Replies Last Post
Gallery folders, different views? millski Graphics & Video & Streaming 0 2007-06-02 14:36
Display, modify and save image mazi888 Symbian Media (Graphics & Sounds) 1 2006-03-13 20:14
Drawing on an image scottleaw Mobile Java Media (Graphics & Sounds) 2 2007-09-26 18:57
Cut an image antonio5982 Mobile Java Media (Graphics & Sounds) 6 2007-09-20 11:17
Transferring jpeg fiel to PC ja_calulot General Symbian C++ 5 2003-09-09 03:08
 
Powered by MediaWiki