×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    47

    how to save byte image to phone memory

    hi, as i have use the gif encoder and made a gif image out according to the codes below. Once the user select the save command, it will generetate a byte[], with this, how am i able to save the byte[] into a image in the phone memory?


    Code:
     public void commandAction(Command c, Displayable d) {
            
            if(c==bkCmd) {
                mainCanvas mainCanvas= new mainCanvas( midlet, display , background,text,  x, y, fontSize, fontColour,  mustRepaintText,addChoice, characterOne,  characterTwo, fontStyle, backgroundName);
                display.setCurrent(mainCanvas);
                
            }else if(c==saveCmd) {
                
                 frameImage1= createImageMethod(1);
                frameImage2=createImageMethod(2);
                frameImage3=createImageMethod(3);
               byte[] gifImage= saveMethod(frameImage1,frameImage2,frameImage3);
               System.out.println(gifImage.toString()+"GIF IMAGE DATA");
              
             testing= Image.createImage(gifImage,0,gifImage.length);
               
               
                repaint();
            }
            
            
        }

  2. #2
    Nokia Developer Champion
    Join Date
    Sep 2007
    Posts
    915

    Re: how to save byte image to phone memory

    If you will convert that byte array to string then it will convert to acii value of perticulat elemeent. Then you can easily store it to the phone memory in RMS.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    47

    Re: how to save byte image to phone memory

    hi, i am actually confuse, you suggest using RMS, then wat about FileConnection API?

  4. #4
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Thumbs up Re: how to save byte image to phone memory

    Quote Originally Posted by intheworldofmyown View Post
    how am i able to save the byte[] into a image in the phone memory?
    Well
    Moving color wise -
    1 - RED - MAKE SENSE TO ME..
    2 - BLUE Following the first red string does not make sense..
    3 - RED make sense,

    I guess just you have converted the gif image in byte array and now you wana save the same..correct..

    Yes you can save that image in RMS -
    The code is follow -

    import javax.microedition.lcdui.*;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import javax.microedition.rms.InvalidRecordIDException;
    import javax.microedition.rms.RecordEnumeration;
    import javax.microedition.rms.RecordStore;

    /**
    *
    * Implements methods to store and load Images to/from RMS,
    * expire images, and clear the RMS local store.
    *
    */
    public final class ImageRmsUtils {

    /**
    * Saves a PNG Image//Byte Array
    *
    * @param resourceName is the name of the PNG image to save.
    * @param image the Image to save.
    */
    static public void savePngImage(String recordStore, String resourceName, Image image) {
    RecordStore imagesRS = null;
    int height, width;
    if (resourceName == null) return; // resource name is required

    // Calculate needed size and allocate buffer area
    height = image.getHeight();
    width = image.getWidth();

    int[] imgRgbData = new int[width*height];

    try {
    image.getRGB(imgRgbData, 0, width, 0, 0, width, height);
    imagesRS = RecordStore.openRecordStore(recordStore, true);

    //
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    DataOutputStream dout = new DataOutputStream(bout);
    // Serialize the image name
    dout.writeUTF(resourceName);
    dout.writeInt(width);
    dout.writeInt(height);
    dout.writeLong(System.currentTimeMillis());
    dout.writeInt(imgRgbData.length);
    // Serialize the image raw data
    for (int i=0; i<imgRgbData.length; i++) {
    dout.writeInt(imgRgbData[i]);
    }
    dout.flush();
    dout.close();
    byte[] data = bout.toByteArray();
    int recid = imagesRS.addRecord(data, 0, data.length);
    } catch (Exception e) {
    // Log the exception
    } finally {
    try {
    // Close the Record Store
    if (imagesRS != null) imagesRS.closeRecordStore();
    } catch (Exception ignore) {
    // Ignore
    }
    }
    }

    /**
    * Load image with specified name
    *
    * @param recordStore is the name of the record store.
    * @param resourceName is the name of the image to load
    *
    * @return the loaded Image or null.
    */
    static public Image loadPngFromRMS(String recordStore, String resourceName) {
    RecordStore imagesRS = null;
    Image img = null;
    try {
    imagesRS = RecordStore.openRecordStore(recordStore, true);
    RecordEnumeration re = imagesRS.enumerateRecords(null, null, true);
    int numRecs = re.numRecords();
    // For each record
    for(int i=0; i<numRecs; i++) {
    // Get the next record's ID
    int recId = re.nextRecordId(); // throws InvalidRecordIDException
    // Get the record
    byte[] rec = imagesRS.getRecord(recId);
    //
    ByteArrayInputStream bin = new ByteArrayInputStream(rec);
    DataInputStream din = new DataInputStream(bin);
    String name = din.readUTF();
    // If this is the image we are looking for, load it.
    if (name.equals(resourceName)== false) continue;

    int width = din.readInt();
    int height = din.readInt();
    long timestamp = din.readLong();
    int length = din.readInt();

    int[] rawImg = new int[width*height];
    // Serialize the image raw data
    for (i = 0; i < length; i++) {
    rawImg[i] = din.readInt();
    }
    img = Image.createRGBImage(rawImg, width, height, false);
    din.close();
    bin.close();
    }
    } catch (InvalidRecordIDException ignore) {
    // End of enumeration, ignore
    } catch (Exception e) {
    // Log the exception
    } finally {
    try {
    // Close the Record Store
    if (imagesRS != null) imagesRS.closeRecordStore();
    } catch (Exception ignore) {
    // Ignore
    }
    }
    return img;
    }

    Check this code and you can modify the same as per the need,
    For file connection check this -
    http://www.forum.nokia.com/info/sw.n..._0_en.zip.html
    Thanks,
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  5. #5
    Nokia Developer Champion
    Join Date
    Sep 2007
    Posts
    915

    Re: how to save byte image to phone memory

    Quote Originally Posted by intheworldofmyown View Post
    hi, i am actually confuse, you suggest using RMS, then wat about FileConnection API?
    Some old phones may not support File Connection API so you can use rms for that data. You can also store string into rms and in File System.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    47

    Re: how to save byte image to phone memory

    hi Raj, the codes you gave me is to saving my image into a record store then transfer it into phone by FileConnectionAPI?

    Am i doing the correct way?

    my codes are as below
    Code:
    else if(c==saveCmd) {
                
                frameImage1= createImageMethod(1);
                frameImage2=createImageMethod(2);
                frameImage3=createImageMethod(3);
    // this is the part where i generate my gif image by the gif encoder
                byte[] gifImage= saveMethod(frameImage1,frameImage2,frameImage3);            System.out.println(gifImage.toString()+"GIF IMAGE DATA");
                
    //this is to create the gifImage to be save into the phone.
                createFile(gifImage);            testing= Image.createImage(gifImage,0,gifImage.length);
                
                
                repaint();
            }
    
     public void createFile(byte[] gifImage) {
            try {
    Connector.open("file:///SDCard/TestImage.gif",Connector.WRITE);
         FileConnection fconn = (FileConnection)Connector.open("file://c:/testing/TestImage.gif",Connector.WRITE);
                           
    // Always check whether the file or directory exists.
                // Create the file if it doesn't exist.
                if(!fconn.exists()) {
                    fconn.create();
                    
                    DataOutputStream dos = fconn.openDataOutputStream();
                    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                    DataOutputStream outputDataStream= new DataOutputStream(outputStream);
                    for(int i=0;i<gifImage.length;i++) {
                        outputDataStream.writeInt(gifImage[i]);
                    }
                    outputDataStream.flush();
                    
                    byte[] outputRecord = outputStream.toByteArray();
                    dos.write(outputRecord);
                    dos.flush();
                    dos.close();
                    
                    fconn.close();
                    
                    
                }
                fconn.close();
            } catch(IOException ioe) {
            }
        }

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    47

    Re: how to save byte image to phone memory

    So i can either use fileConnection API or RMS , either way will be able to save the image to the phone?

  8. #8
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Thumbs up Re: how to save byte image to phone memory

    Hi,
    I gave you two styles of code -
    @ First is to save the image in RMS and then load the same,
    @ second is using file connection..

    It is hardly matter how you have taken the byte array...
    you have byte array ..then just write the same in rms using the fist code.

    However you are writing through the FileConnection...API..ok
    Just check the code ..I gave you,Why dont you check that?
    Thanks,
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    47

    Re: how to save byte image to phone memory

    Hi Raj, sorry for the trouble, u mean by either way i will be able to save it to the phone? Kind of confuse. =)

  10. #10
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Thumbs up Re: how to save byte image to phone memory

    Hi,
    Yeah,
    Thanks,
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  11. #11
    Registered User
    Join Date
    Nov 2008
    Posts
    31

    Re: how to save byte image to phone memory

    Hi Raj, thanks for your time in explaining

    but there's a problem regarding the file connection
    it gave a imageviewer example
    i read and study up the codes all night long last night

    the imageviewer cannot be opened using our platform, netbeans 5.5

    so we tried opening it by creating a new project and copy paste the codes inside

    it worked somehow.

    but at this point, when we run the application it just stuck there

    Then the next plan is we deployed it into a N95 phone straight away
    it worked, but the imageviewer can only let us see the data inside the phone (background/images)
    but it doesnt let us create and write new image into the phone and store it

  12. #12
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Thumbs up Re: how to save byte image to phone memory

    Hi,
    Check whether that function has been provided,
    Thanks,
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  13. #13
    Registered User
    Join Date
    Nov 2008
    Posts
    47

    Re: how to save byte image to phone memory

    Hi Raj, what is the function you are referring to?

  14. #14
    Registered User
    Join Date
    Nov 2008
    Posts
    47

    Re: how to save byte image to phone memory

    Hi, anyway for your information, i want to save the image into phone, and the user will be able to view the image that is save in their picture folder even they are outside the application

  15. #15
    Registered User
    Join Date
    Nov 2008
    Posts
    47

    Re: how to save byte image to phone memory

    Any ideas? cause my codes and running on the emulator, it will have a alert to inform user that the application wants to write the local file system and ask No or YES. And i click yes nothing happens.

    Anyway by doing this, am i able to save my image into the local memory of the phone . Example like in the images folder of a nokia phone?

    Thank you

    Code:
    public void createFile(byte[] gifImage, Image testing) {
          
            
            try {
                FileConnection fconn = (FileConnection)Connector.open("file:///C:/Nokia/Images/Pictures/testing.gif",Connector.WRITE);
                
                if (!fconn.exists()) {
                    fconn.create();
                    DataOutputStream dos=fconn.openDataOutputStream();
                    dos.write(gifImage,0,gifImage.length);
                    dos.flush();
                    dos.close();
                }
                
                else {
                    Alert al=new Alert("Duplicate File Name");
                    al.setString("Image with this name already exists");
                    Display.getDisplay(midlet).setCurrent(al);
                }
                
                fconn.close();
                
            }
            
            catch (IOException e) {
                
                e.printStackTrace();
            }
        }

Page 1 of 2 12 LastLast

Similar Threads

  1. How to use Browser Control to download an image , and then save to phone?
    By caepek in forum Mobile Web Site Development
    Replies: 6
    Last Post: 2009-11-10, 08:31
  2. desperately needs help on how to save an image in the mobile device memory
    By daregazi in forum Mobile Java Media (Graphics & Sounds)
    Replies: 1
    Last Post: 2008-06-23, 12:37
  3. Nokia 6600 phone memory / FExplorer
    By victorh81 in forum General Development Questions
    Replies: 2
    Last Post: 2005-02-08, 15:26
  4. Loading Image data from 'raw' bytes
    By LongSteve in forum Mobile Java General
    Replies: 2
    Last Post: 2002-11-20, 17:38

Posting Permissions

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