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
Developer
Blanc
Posts: 638
Registered: ‎07-02-2009
My Device: Not Specified
Accepted Solution

is it possible to save an image downloaded from net??

hi i need to save an image downloaded from net? how can i make it possible? is it possible to strore it in the RecordStore? and then use it later when we want it?

 

am into a tough deadline please help me!!!

Thanks and Regards,
PraveenGoparaju.
Developer
Sunilkumar
Posts: 75
Registered: ‎07-21-2009
My Device: Not Specified

Re: is it possible to save an image downloaded from net??

Hi ,

 use below code ......

 

 String Imagedirectory= System.getProperty("fileconn.dir.photos");
        	
	  	
	  			
	  			
	  		try
	  		{
	  			System.out.println("Imageurl="+imageurl.trim());
	  			HttpConnection ImagehttpConn = (HttpConnection)Connector.open(imageurl);//,Connector.READ_WRITE, true); 
	  			ImagehttpConn.setRequestMethod(HttpConnection.POST);
	  		//	ImagehttpConn.setRequestProperty("Content-Type", "Image/jpeg");
	  		
	  			int status= ImagehttpConn.getResponseCode();
				
				if(status== HttpConnection.HTTP_OK)
				{
					
				
					is = ImagehttpConn.openDataInputStream();
					
					StringBuffer Imagedata=new StringBuffer();
					 int chr;
					
					
						while ((chr = is.read()) != -1)
						{
							Imagedata.append((char) chr);
						}
						System.out.println("Imagedirectory"+Imagedirectory+imagename);
						writeFile(Imagedata.toString().getBytes(),Imagedirectory+imagename);
					}
				
				if(is!= null)
				is.close();
				 if(ImagehttpConn!=null)
					 ImagehttpConn.close();		
	  			
	  		}
			catch (IOException e)
		       {
				Dialog.alert("IOException2");
		    	  
			} 
  			 


and this for write the image //

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");
	        }
	    }

 

 

Developer
peter_strange
Posts: 19,609
Registered: ‎07-14-2008
My Device: Not Specified

Re: is it possible to save an image downloaded from net??

[ Edited ]

"am into a tough deadline"

 

Not wishing to be rude, but next time I would suggest that you search the forum for answers to your questions.  This question has been answered before and there are many related questions that would have helped you, for example:

 

http://supportforums.blackberry.com/t5/Java-Development/how-to-save-encodedImage-on-disc/m-p/215531#...

 

http://supportforums.blackberry.com/t5/Java-Development/How-to-download-an-image-to-the-device-or-SD...

 

Asking the same question again just bloats this forum with duplicate answers, making it more difficult for all users.

Developer
Blanc
Posts: 638
Registered: ‎07-02-2009
My Device: Not Specified

Re: is it possible to save an image downloaded from net??

 

thanks both of you!!!

 

ya really i didnt searched the forums... anyway thanks for your reply.

Thanks and Regards,
PraveenGoparaju.
Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: is it possible to save an image downloaded from net??

try to use it at least once before opening the next post.

----------------------------------------------------------
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