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
babakar
Posts: 752
Registered: ‎05-29-2012
My Device: see later

Download

FileConnection fc = (FileConnection)Connector.open("file:///SDCard/offline/"+selected.getSongName()+".mp3");
                                          // If no exception is thrown, then the URI is valid, but the file may or may not exist.
                                          if (!fc.exists())
                                          {
                                              fc.create();  // create the file if it doesn't exist
                                          }
                                          OutputStream outStream = fc.openOutputStream();
                                          outStream.write("test content".getBytes());
                                          outStream.close();
                                          fc.close();

 

I have done that, which supposedly will create to me the file song.mp3 in folder sdcard/offline

However, I want from this link http://www.***.com/song.mp3 to read it and write it into the the file song.mp3 in folder sdcard/offline

 

How can this be done?I assume this is how download idea is

Please use plain text.
Developer
simon_hain
Posts: 16,215
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Download

you can check the httpdemo to get a sample for that.
----------------------------------------------------------
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
Please use plain text.
Developer
babakar
Posts: 752
Registered: ‎05-29-2012
My Device: see later

Re: Download

any links for it?
Please use plain text.
Developer
rishikeshsinghpbh
Posts: 121
Registered: ‎05-31-2012
My Device: 9900

Re: Download

for Httpdemo check the samples that comes with jdk...

Rishikesh Singh
Please use plain text.
Developer
babakar
Posts: 752
Registered: ‎05-29-2012
My Device: see later

Re: Download

FileConnection fc = (FileConnection)Connector.open("file:///SDCard/offline/"+selected.getSongName()+".mp3");
										//FileConnection fc2 = (FileConnection)Connector.open(resulto);
										
									      // If no exception is thrown, then the URI is valid, but the file may or may not exist.
									      if (!fc.exists())
									      {
									          fc.create();  // create the file if it doesn't exist
									      }
									      OutputStream outStream = fc.openOutputStream(); 
									      HttpConnection conn = (HttpConnection) Connector.open(resulto, 
									                 Connector.READ_WRITE, true);
									                conn.setRequestMethod(HttpConnection.GET);
									       InputStream  in = conn.openInputStream();
									       int val;
									       while ((val = in.read()) != -1)  
									    	    outStream.write(val); 
									       
							                in.close();
							                outStream.close();
							                conn.close();

 

 

this is what am doing and its crashing. first create the file, then read from inputstream to outputstream

Please use plain text.
Developer
babakar
Posts: 752
Registered: ‎05-29-2012
My Device: see later

Re: Download

here is what I do, and it doesnt work

 

FileConnection fc = (FileConnection)Connector.open("file:///SDCard/offline/"+selected.getSongName()+".mp3");
										
									      // If no exception is thrown, then the URI is valid, but the file may or may not exist.
									      if (!fc.exists())
									      {
									          fc.create();  // create the file if it doesn't exist
									      }
									      OutputStream outStream = fc.openOutputStream(); 
									      HttpConnection conn = (HttpConnection) Connector.open(resulto, 
									                 Connector.READ_WRITE, true);
									                conn.setRequestMethod(HttpConnection.GET);
									       InputStream  in = conn.openInputStream();
									       int val;
									       while ((val = in.read()) != -1)  
									    	    outStream.write(val); 
									       
	      
							                in.close();
							                outStream.close();
							                conn.close();
									        fc.close();

 

Please use plain text.
Developer
babakar
Posts: 752
Registered: ‎05-29-2012
My Device: see later

Re: Download

it creates the file, but it doesnt read from the url link to write in file
Please use plain text.
Developer
simon_hain
Posts: 16,215
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Download

try single parts, don't put everything together until it works.
----------------------------------------------------------
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
Please use plain text.
Developer
babakar
Posts: 752
Registered: ‎05-29-2012
My Device: see later

Re: Download

FileConnection fc = (FileConnection)Connector.open("file:///SDCard/offline/"+selected.getSongName()+".mp3",Connector.READ_WRITE);
										

 if (!fc.exists())
									      {
									          fc.create();  // create the file if it doesn't exist
									      }
									      OutputStream outStream = fc.openOutputStream(); 
									      HttpConnection conn = (HttpConnection) Connector.open(resulto, 
									                 Connector.READ_WRITE, true);
									                conn.setRequestMethod(HttpConnection.GET);
									       InputStream  in = conn.openInputStream();
									       byte buf[]=new byte[1024];
									       int len;

 while((len=in.read(buf))>0){ outStream.write(buf); }
	      
									       outStream.close();
									       in.close();
							              
							                conn.close();
									        fc.close();

 

 

I did it single part, everything is working. only thing not working is bolded part...its not filling the file with data from url

Please use plain text.