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. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
Developer
hades_6
Posts: 178
Registered: ‎08-26-2009
Accepted Solution

Image not saving on real device but works perfectly on emulator

Hi guys, I'm using the 4.6.1 JDE. I want to store a downloaded image on the file system and retrieve it later. Something like a caching option. Everything works perfectly on the simulator but when I install on the device it doesn't work. Here's the code,

 

 

private StringBuffer Imagedirectory = new StringBuffer(System.getProperty("fileconn.dir.photos"));



FileConnection fc = (FileConnection)Connector.open(Imagedirectory.toString(),Connector.READ_WRITE);
                                
                if(!fc.exists()){
                    Util.getWebData(url, this);
                }else{            
                    
                    byte[] data = getData(fc);
                    
                    bitmap = EncodedImage.createEncodedImage(data, 0,  
                            data.length);
                    
                    if(this.width !=0 && this.height !=0){    
                        bitmap = ScaleImageToSize(bitmap, this.width,this.height);
                    }
                    setImage(bitmap);
 }

this is where I get the data from the file,

 

private byte[] getData(FileConnection file)
    {        
        byte[] data = new byte[0];
        try
        {           
        	 InputStream input = file.openInputStream();
                          //////////
             int SIZE = 100000;
             ByteArrayOutputStream byteArrayOutputStream = new 
                                ByteArrayOutputStream();
             byte[] buffer = new byte[SIZE];
             while (true) 
             {
               int bytesRead = input.read( buffer, 0, SIZE );
               if (bytesRead == -1) break;
               byteArrayOutputStream.write( buffer, 0, bytesRead );
             }
             data = byteArrayOutputStream.toByteArray();
             byteArrayOutputStream.flush();
             byteArrayOutputStream.close();

            
        }
        catch(Exception e)
        {
            System.out.println(e.toString());            
        }         
        return data;
		
    }  

 this is where I write the image to a file,

 

 

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);
				out.flush();
			} catch (IOException e) {
				System.out.print("Error writing to output stream");
			}

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

 

Please help.

 

 

Please use plain text.
Developer
rcmaniac25
Posts: 1,789
Registered: ‎04-28-2009
My Carrier: Verizon

Re: Image not saving on real device but works perfectly on emulator

Are you getting an exception (or the println output specifying and error) or does everything run perfect and you just can't find the file?

------------------------------------------------------------
Three simple rules:
1. Please use the search bar before making new posts.
2. Kudo posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Please use plain text.
Developer
peter_strange
Posts: 17,713
Registered: ‎07-14-2008

Re: Image not saving on real device but works perfectly on emulator

And what doesn't work?

Please use plain text.
Developer
hades_6
Posts: 178
Registered: ‎08-26-2009

Re: Image not saving on real device but works perfectly on emulator

[ Edited ]

Thank you so much for replying.

 

I found the problem, the byte array which I was parsing was not proper, so I first created the image and after that used the  byte information from that to save the image. Here's the solution.

 

 

 

	public void callback(final String data)  
	{  
		if (data.startsWith("Exception")) return;  

		try  
		{ 
			
			byte[] dataArray = data.getBytes();
			bitmap = EncodedImage.createEncodedImage(dataArray, 0,  
					dataArray.length);
			
			if(this.width !=0 && this.height !=0){	
				bitmap = ScaleImageToSize(bitmap, this.width,this.height);
			}
			setImage(bitmap);			
			
			if(isWriteImage()){
				
				System.out.println("Before writing the image");
				writeFile(bitmap.getData(), Imagedirectory.toString());
				System.out.println(Imagedirectory);			
				System.out.println("Image written - Operation Successful");
			}
			
		}  
		catch (final Exception e){
			System.out.println("Image Creation Failed");			
		}  
	}  

 

The problem was because I was parsing the byte array from the string variable data. Any idea why this caused an issue?

 

 

Please use plain text.