×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Regular Contributor
    Join Date
    Mar 2003
    Location
    Hyderabad.
    Posts
    99

    Unable same image into gallery after converting image to bytearray

    Hi,

    I created an image from my resource and received an image object which i converted into
    byte array using the below method and tried to save it into Gallery using FileConnection
    and the image is not saved properly.

    please help me on how to get an original byte array from an image object to save it into gallery.

    public static byte[] getByteArray(Image image) {

    // Allocate new memory for the int-array and fill the array in the getRGB method.
    int[] raw = new int[image.getWidth() * image.getHeight()];
    image.getRGB(raw, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());

    byte[] rawByte = new byte[image.getWidth() * image.getHeight() * 4];// ARGB

    int a, r, g, b;
    int n = 0;

    // loop through the int-array and get the 4 bytes of each value to the byte-array.
    // since java isn't supporting unsigned bytes we need to store the ARGB values from -128 to 127
    // where -128 = 128, -127 = 129 and -1 = 255
    for (int i = 0; i < raw.length; i++) {

    // Right-shift the values to fit in a byte.
    int ARGB = raw[i];
    a = (ARGB & 0xFF000000) >> 24;
    r = (ARGB & 0xFF0000) >> 16;
    g = (ARGB & 0xFF00) >> 8;
    b = (ARGB & 0xFF);

    rawByte[n] = (byte) b;
    rawByte[n + 1] = (byte) g;
    rawByte[n + 2] = (byte) r;
    rawByte[n + 3] = (byte) a;
    n += 4;
    }

    raw = null;
    return rawByte;
    }

    Regards,
    Basu

  2. #2
    Super Contributor
    Join Date
    Sep 2008
    Location
    Noida, U.P.
    Posts
    1,330

    Thumbs up Re: Unable same image into gallery after converting image to bytearray

    As the image is located in resources then its better to use getResourceAsStream().

    Code:
    
    import java.io.InputStream;
    import java.io.OutputStream;
    import javax.microedition.io.Connector;
    import javax.microedition.io.file.FileConnection;
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    
    /**
     * @author jitendra
     */
    public class Fileread extends MIDlet implements Runnable 
    {
    
        Thread thread;
    
        public void startApp() {
            thread = new Thread(this);
            thread.start();
        }
    
        public void pauseApp() {
        }
    
        public void destroyApp(boolean unconditional) {
        }
        
        FileConnection fileConnection1;
        InputStream inputStream;
        OutputStream outputStream;
    //    StringBuffer buffer = new StringBuffer();
        byte [] imageBytes = null;
        public void run() {
    
            try {
    
                fileConnection1 = (FileConnection) Connector.open("file:///root1/Playmate128x128.png",Connector.READ_WRITE);
                if (!fileConnection1.exists())
                    fileConnection1.create();
    //            inputStream = fileConnection1.openInputStream();
                inputStream = getClass().getResourceAsStream("/img.png");
                outputStream = fileConnection1.openOutputStream();
                int i;
                while ((i = inputStream.read()) != -1) {
    //                buffer.append((char)i);
                    outputStream.write(i);
                }
    //            imageBytes = buffer.toString().getBytes();
                outputStream.flush();
            } catch (Exception exception) {
                System.out.println("exception---"+exception.toString());
                exception.printStackTrace();
            } finally {
                try {
                    if (inputStream != null) {
                        inputStream.close();
                        outputStream.close();
                        outputStream = null ;
                        fileConnection1.close();
                        inputStream = null;
                        fileConnection1 = null;
                    }
                } catch (Exception exception) {
                }
            }
        }
    }
    
    Use the following code..
    thanks,
    jitu_goldie..

    KEEP TRYING..

Similar Threads

  1. Replies: 1
    Last Post: 2008-07-11, 02:32
  2. Unable to veiw image in nokia 9300 browser
    By parthmankad in forum Symbian
    Replies: 0
    Last Post: 2006-10-11, 13:22
  3. Unable to View image in Nokia 9300
    By parthmankad in forum Symbian Networking & Messaging (Closed)
    Replies: 0
    Last Post: 2006-10-11, 13:20
  4. Unable to draw image when clicked
    By ckt in forum Symbian
    Replies: 1
    Last Post: 2006-09-30, 07:52

Posting Permissions

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