views:

1789

answers:

3

I have a ready Bitmap image. Now I want to save & delete it. I m doing it like..

FileConnection fconn = (FileConnection)Connector.open("file:///store/home/user/StoredBitmap/"+picIndex+".bmp",Connector.READ_WRITE);

        if(!fconn.exists())
            fconn.create();

        OutputStream out = fconn.openOutputStream();

       if(image == null)
            System.out.println("    image null  ");
       else
       {
            out.write(byte[]);
            out.close();
       }

        fconn.close();

For Blackberry Storm device & simulator, which path to give instead of

"file:///store/home/user/StoredBitmap/"+picIndex+".bmp"

I have a created Bitmap. In outputStream, how to write it?

I m using Blackberry 4.7 (Version: 4.7.0.41). In its simulator, how to save the Bitmap? I m doing it for Blackberry Storm.

& for deleting that Bitmap, can we use File class or we've to use FileConnection class?

+1  A: 

Take a look at this (write/read/delete)

class Scr extends MainScreen implements FieldChangeListener {
    ButtonField mWrite;
    ButtonField mRead;
    ButtonField mDelete;

    String mFileName = System.getProperty("fileconn.dir.photos") 
         + "test.bmp";

    public Scr() {
     mWrite = new ButtonField("Write file", 
              ButtonField.CONSUME_CLICK);
     add(mWrite);
     mWrite.setChangeListener(this);

     mRead = new ButtonField("Read file", 
              ButtonField.CONSUME_CLICK);
     add(mRead);
     mRead.setChangeListener(this);

     mDelete = new ButtonField("Delete file", 
              ButtonField.CONSUME_CLICK);
     add(mDelete);
     mDelete.setChangeListener(this);
    }

    public void fieldChanged(Field field, int context) {
     if (mWrite == field) {
      byte[] bytes = new byte[] { 1, 2, 1, 1 };
      writeFile(bytes, mFileName);
      Dialog.inform("File " + mFileName + " saved");
     } else if (mRead == field) {
      byte[] bytes = readFile(mFileName);
      if (null != bytes)
       Dialog.inform("File " + mFileName + " opened");
     } else if (mDelete == field) {
      deleteFile(mFileName);
      Dialog.inform("File " + mFileName + " deleted");
     }

    }

    private void writeFile(byte[] data, String fileName) {
     FileConnection fconn = null;
     try {
      fconn = (FileConnection) Connector.open(fileName,
        Connector.READ_WRITE);
     } catch (IOException e) {
      System.out.print("Error opening file");
     }

     if (fconn.exists())
      try {
       fconn.delete();
      } catch (IOException e) {
       System.out.print("Error deleting file");
      }
     try {
      fconn.create();
     } catch (IOException e) {
      System.out.print("Error creating file");
     }
     OutputStream out = null;
     try {
      out = fconn.openOutputStream();
     } catch (IOException e) {
      System.out.print("Error opening output stream");
     }

     try {
      out.write(data);
     } catch (IOException e) {
      System.out.print("Error writing to output stream");
     }

     try {
      fconn.close();
     } catch (IOException e) {
      System.out.print("Error closing file");
     }
    }

    private byte[] readFile(String fileName) {
     byte[] result = null;
     FileConnection fconn = null;
     try {
      fconn = (FileConnection) Connector.open(fileName, 
                   Connector.READ);
     } catch (IOException e) {
      System.out.print("Error opening file");
     }

     if (!fconn.exists()) {
      Dialog.inform("file not exist");
     } else {

      InputStream in = null;
      ByteVector bytes = new ByteVector();
      try {
       in = fconn.openInputStream();
      } catch (IOException e) {
       System.out.print("Error opening input stream");
      }

      try {
       int c = in.read();
       while (-1 != c) {
        bytes.addElement((byte) c);
        c = in.read();
       }
       result = bytes.getArray();
      } catch (IOException e) {
       System.out.print("Error reading input stream");
      }

      try {
       fconn.close();
      } catch (IOException e) {
       System.out.print("Error closing file");
      }
     }
     return result;
    }

    private void deleteFile(String fileName) {
     FileConnection fconn = null;
     try {
      fconn = (FileConnection) Connector.open(fileName,
        Connector.READ_WRITE);
     } catch (IOException e) {
      System.out.print("Error opening file");
     }

     if (!fconn.exists()) {
      Dialog.inform("file not exist");
     } else {
      try {
       fconn.delete();
      } catch (IOException e1) {
       System.out.print("Error deleting file");
      }

      try {
       fconn.close();
      } catch (IOException e) {
       System.out.print("Error closing file connection");
      }
     }
    }
Max Gontar
A: 

I did the same as ur code for buttonFields.

But no effect for button touch only its invoked if i get focus on it & click Enter. I m testing it on Blackberry 4.7.0.41

& I m saving the image like..

private void saveBitmap(int picIndex, Bitmap bmp) 
{
    try
    {
        FileConnection fconn = (FileConnection)Connector.open(PHOTO_DIR + picIndex + EXTENSION, Connector.READ_WRITE); 

        if(!fconn.exists())
            fconn.create();

        OutputStream out = fconn.openOutputStream();
        byte[] bitmapBuffer = getBytesFromBitmap(bmp);
        Bitmap bitmapImage = Bitmap.createBitmapFromBytes(bitmapBuffer,0,bitmapBuffer.length,1);
        //EncodedImage encodedImage =  EncodedImage.createEncodedImage(bitmapBuffer,0,bitmapBuffer.length);
        //Bitmap bitmapImage = encodedImage.getBitmap();

       if(bitmapImage == null)
            System.out.println("    image null  ");
       else
       {
            out.write(bitmapBuffer);
            out.close();
       }

        fconn.close();
    }
    catch(Exception e){
        System.out.println("  Exception while saving Bitmap:: "+e.toString());
        e.getMessage();
    }
}


public byte[] getBytesFromBitmap(Bitmap bmp) { 
try {
        int height=bmp.getHeight();
        int width=bmp.getWidth();
        int[] rgbdata = new int[width*height];
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bos);
        //Graphics g = new Graphics(bmp);
        bmp.getARGB(rgbdata,0,width,0,0,width,height);

        for (int i = 0; i < rgbdata.length ; i++) {
            if (rgbdata[i] != -1)
            {
                dos.writeInt(rgbdata[i]);
                dos.flush();
                }
            } 
        bos.flush();
        return bos.toByteArray(); 
    } catch (Exception ex) {
             return null; } 
}

But

java.lang.IllegalArgumetException

is coming for

Bitmap.createBitmapFromBytes(bitmapBuffer,0,bitmapBuffer.length,1);
&
EncodedImage encodedImage =    EncodedImage.createEncodedImage(bitmapBuffer,0,bitmapBuffer.length);
        Bitmap bitmapImage = encodedImage.getBitmap();

Any clues why this Exception is coming?

Shreyas
donno. Can you debug and look into values of bitmapBuffer and encodedImage?
Max Gontar
and also, if you use getARGB() to save Bitmap, I think its a good idea to create new Bitmap and setARGB() when you read it.
Max Gontar
is my method getBytesFromBitmap(Bitmap bmp) posted above is correct?if not Then How to get byte[] from a Bitmap?
Shreyas
A: 

I m saving Bitmap image like..

private void saveBitmap(int picIndex, Bitmap bmp) 
{
try
{
 String PHOTO_DIR = System.getProperty ("fileconn.dir.photos"); 
 String EXTENSION = ".bmp";
 String filePath = PHOTO_DIR + picIndex + EXTENSION;

 FileConnection fconn = (FileConnection)Connector.open(filePath, 
                               Connector.READ_WRITE); 

 if(!fconn.exists())
     fconn.create();

 OutputStream outputStream = fconn.openOutputStream();

 // bitmap to byte array conversion    
 PNGEncoder encoder = new PNGEncoder(bmp, true);
 byte[] bitmapBuffer = encoder.encode(true);

 outputStream.write(bitmapBuffer);
 outputStream.close();
 fconn.close();
}
catch(Exception e)
{
 System.out.println("  Exception while saving Bitmap:: "+e.toString());
}
}

, the bimap is saved in Media->Pictures->Pictures Folder->1.bmp

but when clicked on it to view, error comes on console as

Unable to create EncodedImage for: /store/home/user/pictures/1.bmp

I m working in Blackberry JDE 4.7.0.41 & tested in Blackberry 9500, 9530 Simulators & Strom 9500 device but its not working. The same above problem is coming.

Not getting what may be the problem. Plz suggest.

Shreyas
Shreyas