• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps downloading file from URL and Caching in Android

kaancho

Newbie
Hi,
I have trying unsuccessfully to download content from URL and caching it ?

Does anyone have example on how to do it?
Does the download need to be defined as a service?

Is this one way to do it -
1) Download and caching is defined as a service
2) Activity starts the service and waits upon service to complete.
3) Once the service completes it notifies the activity and activity continues.
 
If you need to repeatedly download a file, then yes, a service would be your best bet. If you only need to download the file once each time the app is run, then you don't need to use a service. A simple Activity will suffice in this case.

Basically you would do something like:

Code:
URL address = URL.parse("http://yoururlhere.com/yourfile.txt")
URLConnection conn = new URLConnection(address);
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer bab = new ByteArrayBuffer(64);
int current = 0;
while((current = bis.read()) != -1)
{
         bab.append((byte)current);
}

FileOutputStream fos = new FileOutputStream(new File(filepath));
fos.write(bab.toByteArray());
fos.close();

There are some try/catch blocks required as there are a few expecitons thrown, but I don't remember which exceptions are thrown or which lines throw the exceptions off the top of my head, but I am sure you cna figure that much out.
 
while using this code i get the error:

"Default buffer size used in BufferedInputStream constructor. It would be better to be explicit if an 8k buffer is required."

Code:
                String fileName = "filename";
    		URL url = new URL("some url");
			File file = new File(fileName);
			
			URLConnection ucon = url.openConnection();
			InputStream is = ucon.getInputStream();
			
			BufferedInputStream bis = new BufferedInputStream(is);
			ByteArrayBuffer baf = new ByteArrayBuffer(50);
			
			int current = 0;
			while ((current = bis.read()) != -1) {
				baf.append((byte) current);
			}
			FileOutputStream fos = new FileOutputStream(file);
			fos.write(baf.toByteArray());
			fos.close();
Any idea how to fix this?
 
I changed to the

Code:
BufferedInputStream bis = new BufferedInputStream(is, 8096);

and i dont get the previously mentioned warning.
however, i dont seem to get past the

Code:
FileOutputStream fos = new FileOutputStream(file);

Is there something I am missing ?
 
Create a new AVD and in the creation window, under the "Harware: " field, click the "New" button and choose "SD Card support".

Then, under the "SD Card" field, ensure that the "Size" radio button is clicked and enter a size fro the SD card.

This will allow the App to download stuff to the sd card. I also recommend that you test it on a real device as the emulator isn't really that great for testing.
 
Hi,
I created another AVD with 16MB (8MB wouldnt start the AVD for some reason).

I created a basic AVD and then tried to start my program....but I am not sure what i might be doing wrong.

Code:
I/ActivityManager(   61): Start proc con.android.readfile for activity con.android.readfile/.ReadFile: pid=316 uid=10023 gids={3003, 1015}
E/AndroidRuntime(  309): ERROR: thread attach failed
D/dalvikvm(  309): LinearAlloc 0x0 used 636716 of 5242880 (12%)
D/ddm-heap(  316): Got feature list request
I/System.out(  316): xxxxxxxxxxxxxxxxxx
E/BACKGROUND_PROC(  316): /test.json

There is another system out that is supposed to be present after the
Code:
FileOutputStream fos = new FileOutputStream(file);
but I dont see it.

any thoughts?
 
i logged into the adb shell and saw that there was a directory called "sdcard".

Once i used that directory to store the temporary file the file download seems to work.

However, i am not sure if i would need to use directory name specifically (ex. use "cache/filename", "sdcard/filename) to be able to store files there?


Thank you for all your help.
 
Oops, missed a few previous posts. Yes, you need to store it on your sdcard. The internal memory is read-only and can only be accessed if rooted.

This:
Code:
File file = new File(fileName);
Should look like this:
Code:
File file = new File("/sdcard/fileName.jpg"); //fileName.jpg is an example and can be whatever you want it to be called and whatever extension you need.
 
Back
Top Bottom