java left logo
java middle logo
java right logo
 

Home arrow Java ME Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4093
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to store/retrieve image in/from RMS E-mail
User Rating: / 44
PoorBest 

The example below saves JPG file into RMS and shows it on mobile. Data like image, image name, width, height can be stored in RMS. The size of the image file do matters if phone capacity is small. Also some mobiles do not support JPG/GIF type images. In those cases, the image(s) cannot be stored.

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.*;
import java.io.*;

public class ImageStore extends MIDlet  implements CommandListener {
    
    private Command CmdExit;
    private Command CmdOpen;
    private Command CmdBack;
    private Command CmdSave;
    private Display display;
    RecordStore rStore;
    Form form = null;
    Image image = null;
    InputStream is =null;
    
    public ImageStore() {
        
        rStore = null;
        
        display = Display.getDisplay(this);
        
        CmdExit = new Command("Exit"12);
        CmdOpen = new Command("Show"13);
        CmdBack = new Command("Back"13);
        CmdSave = new Command("Save"13);
        
        form = new Form("Image Show");
        
    }
    
    public void startApp() {
        try {
            rStore = RecordStore.openRecordStore("imagefile"true);
        catch(RecordStoreException recordstoreexception) {
            recordstoreexception.printStackTrace();
        }
        try {
            
            is = getClass().getResourceAsStream("/leaf.jpg");
            image = Image.createImage(is);            
            form.append(image);
            
        catch(IOException ioexception) { }
        form.addCommand(CmdExit);
        form.addCommand(CmdSave);
        form.addCommand(CmdOpen);
        form.setCommandListener(this);
        display.setCurrent(form);
    }
    
    public void pauseApp() {
    }
    
    public void Close() {
        try {
            rStore.closeRecordStore();
        catch(RecordStoreNotOpenException recordstorenotopenexception) {
            recordstorenotopenexception.printStackTrace();
        catch(RecordStoreException recordstoreexception) {
            recordstoreexception.printStackTrace();
        }
    }
    
    public void destroyApp(boolean flag) {
        Close();
    }
    
    public Image load(int width,int height) {
        
        byte[] b = null;
        String imagename = null;
        Image image = null;
        
        try {
            
            int i = rStore.getNumRecords();
            
            for(int j = 1; j < i + 1; j++) {
                
                if(rStore.getRecord(j!= null) {
                    
                    b = rStore.getRecord(j);
                    ByteArrayInputStream  bin =  
                            new ByteArrayInputStream);
                    
                    DataInputStream   din = new DataInputStreambin );
                    
                    imagename = din.readUTF();
                    int remaining = 
                            (b.length-imagename.getBytes().length-2)/4;
                    
                    int[] rawdata = new int[remaining];
                    
                    for(int k =;k<rawdata.length ;k++) {
                        rawdata[k= din.readInt();
                    }
                    
                    image = Image.createRGBImage(rawdata,
                            width, height, false);
                    
                    bin.reset();
                    din.close();
                    din =null;
                }
            }
        catch (IOException e) {
            
            e.printStackTrace();
            
        catch(RecordStoreException recordstoreexception) {
            
            recordstoreexception.printStackTrace();
            
        }
        
        return image;
    }
    
    public boolean save(Image img, int width,             
            int height, String imgName) {
        
        if (img == null || width < || height < || imgName == null) {
            
            throw new IllegalArgumentException("Check arguments");
            
        }
        
        int[] imgRgbData = new int[width * height];
        
        try {
            
            img.getRGB(imgRgbData, 0, width, 00, width, height);
            
        catch (Exception e) {
            // Problem getting image RGB data
            return false;
        }
        try {
            // Write image data to output stream (in order to get 
            // the record bytes in needed form)
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DataOutputStream dos = new DataOutputStream(baos);
            dos.writeUTF(imgName);
            
            for (int i = 0; i < imgRgbData.length; i++) {
                dos.writeInt(imgRgbData[i]);
            }
            
            // Open record store, create if it doesn't exist
            rStore.addRecord(baos.toByteArray()0
                    baos.toByteArray().length);  // Add record
            
        catch (RecordStoreNotFoundException rsnfe) {
            // Record storage not found
            return false;
        catch (RecordStoreException rse) {
            // Other record storage problem
            return false;
        catch (IOException ioe) {
            // Problem writing data
            return false;
        }
        
        return true// We've successfuly done
    }
    
    public void commandAction(Command command, Displayable displayable) {
        
        if(command == CmdExit) {
            
            destroyApp(true);
            notifyDestroyed();
            
        
        else if(command == CmdOpen) {
            
            Form showform = new Form("Image from DB");
            Image i = load(image.getWidth(),image.getHeight());
            
            if(i !=null ) {
                
                Image img = Image.createImage(i);
                showform.append(img);
                
            }
            
            showform.addCommand(CmdBack);
            showform.setCommandListener(this);
            display.setCurrent(showform);
            
            else if(command == CmdBack) {
            
                display.setCurrent(form);
                
            else  if(command == CmdSave) {
            
            byte[] b = null;
            Alert a =new Alert("Image saved");
            
            try {
                if(save(image,image.getWidth(),image.getHeight(),"leaf"))
                    a.setString("Success");
                else
                    a.setString("Failed");
                a.setTimeout(1000);
            catch (Exception e) {
                e.printStackTrace();
            }
            display.setCurrent(a);
            }
    }
}

 Related Tips


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.