如何在MIDlet程序中保存/读取一个JPG图像到/从RMS

下边的例子说明了如何保存一个JPG文件到RMS中,请注意,如果移动设备的存储空间太小,将图像文件存储到RMS里可能不是个很好的选择,另外有些移动设备并不支持jpg和gif格式的文件

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", 1, 2);
        CmdOpen = new Command("Show", 1, 3);
        CmdBack = new Command("Back", 1, 3);
        CmdSave = new Command("Save", 1, 3);
       
        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( b );
                   
                    DataInputStream   din = new DataInputStream( bin );
                   
                    imagename = din.readUTF();
                    int remaining =
                            (b.length-imagename.getBytes().length-2)/4;
                   
                    int[] rawdata = new int[remaining];
                   
                    for(int k =0 ;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 < 0 || height < 0 || imgName == null) {
           
            throw new IllegalArgumentException("Check arguments");
           
        }
       
        int[] imgRgbData = new int[width * height];
       
        try {
           
            img.getRGB(imgRgbData, 0, width, 0, 0, width, height);
           
        } catch (Exception e) {
            // 获取图像RGB数据时出错
            return false;
        }
        try {
            // 将图像数据写入输出流
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DataOutputStream dos = new DataOutputStream(baos);
            dos.writeUTF(imgName);
           
            for (int i = 0; i < imgRgbData.length; i++) {
                dos.writeInt(imgRgbData[i]);
            }
           
            // 打开 RecordStore
            rStore.addRecord(baos.toByteArray(), 0,
                    baos.toByteArray().length);  // Add record
           
        } catch (RecordStoreNotFoundException rsnfe) {
            // RecordStore没有被找到
            return false;
        } catch (RecordStoreException rse) {
            // 其他错误
            return false;
        } catch (IOException ioe) {
            // Problem writing data
            return false;
        }
       
        return true; // 操作成功
    }
   
    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);
            }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值