Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
Posts: 120
Registered: ‎05-20-2011
My Device: Touch 9800
My Carrier: Vodafone

Problem in generating bytes of an image

Hello Members .

 

I am using fileJournalChanged() to capture the image from the camera .I able to generate the base64 format of the caputred image but as per my requirements i need the bytes of the image.Now the method for generating the bytes is only returning me [B@2a7f9197 of bytes and this clearly indicates are not complete byte array for the image.I am pasting down my code please correct me where i am wrong:-

 

public void fileJournalChanged()
    {
 
        long nextUSN = FileSystemJournal.getNextUSN();
        String msg = null;
        get();
        for (long lookUSN = nextUSN - 1; lookUSN >= lastUSN && msg == null; --lookUSN)
        {
            FileSystemJournalEntry entry = FileSystemJournal.getEntry(lookUSN);
            if (entry == null)
            {
                break;
            }
            path = entry.getPath();
            
            if (path != null)
            {
                
                     _fileFullName = "file://"+entry.getPath();
                    
                    if ((_fileFullName.endsWith("jpg") || _fileFullName.endsWith("jpeg") || _fileFullName.endsWith("JPEG") || _fileFullName.endsWith("JPG")) && (imgS.equals("yes")))
                    {
                      
                          final byte[] entryByte = _fileFullName.getBytes();
                                      
                           readDataFile(path);// method call    
          
                    }
            }
        }
        lastUSN = nextUSN;



    public final static byte [] readDataFile(String fileName)
    {
    
        InputStream input = null;
        FileConnection fconn = null;
        
        
        try fconn = (FileConnection)Connector.open(fileName, Connector.READ_WRITE);
         
         if (fconn.exists())
         {
          long fileSize = fconn.fileSize();
          readBuffer = new byte [ (int) fileSize];
          
         
          input = fconn.openInputStream();
         byte entry= (byte) input.read(readBuffer);
          System.out.println("======input======================"+entry);
         entry10 = Base64OutputStream.encodeAsString(readBuffer, 0, readBuffer.length, false, false);
         System.out.println(" string 10==============================================="+entry10);
          fconn.delete();
         
         }
        }
        catch (Exception e)
        {
               Dialog.alert("Exception in File Processing" +
                                     ".\nError Exception: " + e.toString());
                  readBuffer = null;
        }
        finally
        {
         try
         {
          if ( input != null )
          {
           input.close();
           input = null;
          }
          if ( fconn != null )
          {
           fconn.close();
           fconn = null;
          }
         }
         catch (Exception e)
         {
          Dialog.alert("Exception in file tidying : " + e.toString());
         }
        }
        System.out.println("reading bufeerrrr======"+readBuffer);
        return readBuffer;
       }
   

You must have notice in my readDataFile() method if when i generate base64 it coming too long but when i decode it using base64 decoder for images the image is generated.But my requirement is bytes.So pleae help me out .

 

Thanks

Amar
Developer
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Problem in generating bytes of an image

Hi hobby_johals,

 

Note that read() does not actually read the all stream.

 

You need to recall this function until it returns -1.

 

One easy way to do it is to use the IOUtilities,streamToBytes() API.

 

E.

New Developer
Posts: 120
Registered: ‎05-20-2011
My Device: Touch 9800
My Carrier: Vodafone

Re: Problem in generating bytes of an image

Thanks for your kind reply maadani.

 

I have also used this method.You talking about this one ?

 

  public static byte[] getBytesFromFile(String filename) throws IOException {
         FileConnection fconn = null;
         InputStream is = null;
         try {
        
             fconn = (FileConnection) Connector.open(filename, Connector.READ);
             is = fconn.openInputStream();

             System.out.println("bytesss-------------"+IOUtilities.streamToBytes(is));
             return IOUtilities.streamToBytes(is);
       
         } finally {
             if (is != null) {
                 is.close();
             }
             if (fconn != null) {
                 fconn.close();
             }
         }
     }

Amar
Developer
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Problem in generating bytes of an image

Yes.

New Developer
Posts: 120
Registered: ‎05-20-2011
My Device: Touch 9800
My Carrier: Vodafone

Re: Problem in generating bytes of an image

but i am getting almost same result

i.e Bytes of the files are .... [B@7464ba9a  this is the o/p .

Amar
Developer
Posts: 17,019
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Problem in generating bytes of an image

it's close. you cannot use it twice. if you want to use it with sysout save it to a variable first.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
New Developer
Posts: 120
Registered: ‎05-20-2011
My Device: Touch 9800
My Carrier: Vodafone

Re: Problem in generating bytes of an image

Thanks but i am getting this much of bytes from that method [B@7464ba9a .Is the bytes fine??

I want to decode it and generate the image.How to debug this thing?

Amar
Developer
Posts: 17,019
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Problem in generating bytes of an image

a byte-array is an object. if you call toString on it it invokes the default object.toString, which gives you this identifier.

You can create a picture from the byte-array using
EncodedImage image = EncodedImage.createEncodedImage(imageData, 0, imageData.length);

see
http://www.blackberry.com/developers/docs/7.0.0api/net/rim/device/api/system/EncodedImage.html
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Problem in generating bytes of an image

Just a heads up, I believe that the listener you are using is invoked before all of the bytes are actually written.  This is NOT the problem you have reported here, Simon's answer is correct for that.  What I am saying is that you might find that you do not read the full image and if you find this, then you have this problem.  To get round this issue I have seen to solutions:

a) Wait until your application is placed in the foreground again before reading this image - your app can't get to the foreground until the camera has finished writing the file.

b) Code a loop in a separate Thread that waits until the file size stop growing, then read the image. 

Highlighted
New Developer
Posts: 120
Registered: ‎05-20-2011
My Device: Touch 9800
My Carrier: Vodafone

Re: Problem in generating bytes of an image

Thanks for your kind suggestions members.Your experience really helps this community.Man Happy

 

Amar