Wednesday, April 6, 2011

RercordStore:-Saving data in mobile

In this tutorial we will learn how to save various file types(video ,audio ,images as well as text ) in a recordstore.Now when you go through the code you'll find
byte[] bytestream = t.getString().getBytes();
here t is a textfield.Hence we save text in this case.
If you want to save image or video ,simply convert it to bytes and then replace t.getString().getBytes() by the bytes you just got.


To View tutorial video Online CLICK HERE
(Download and watch for better quality)
To download Click here


SOURCE CODE:-

 Record.JAVA

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


public class Record extends MIDlet implements CommandListener {

    private Form form;
    private TextField t,t1;
    private Command write,read;
    private RecordStore recordStore;

    public Record()
    {
        form = new Form("Record");
        read = new Command("Read", Command.SCREEN, 1);
        write = new Command("Write", Command.SCREEN, 1);
        form.addCommand(write);
        form.addCommand(read);
        form.setCommandListener(this);
        try {
            recordStore = RecordStore.openRecordStore("My record", true);
        }
        catch (Exception ex) {
        }
        t=new TextField("Enter data","",30,TextField.ANY);
        t1=new TextField("Read data","",30,TextField.ANY);
        form.append(t);
        form.append(t1);
    }

    public void startApp() {
        Display.getDisplay(this).setCurrent(form);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
        if(c==write)
        {
            try {
                byte[] bytestream = t.getString().getBytes();
                recordStore.addRecord(bytestream, 0, bytestream.length);
            }
            catch (Exception ex) {
            }
        }
        if(c==read)
        {
            try {
                byte[] Data = new byte[recordStore.getRecordSize(1)];
                int length = recordStore.getRecord(1, Data, 0);
                t1.setString(new String(Data, 0,length));
            }
            catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}

Delete and update in recordstore is covered in next tutorial.

No comments:

Post a Comment

customised by Vaibhav