downloading file from URL and Caching in Android


Last Updated:

  1. kaancho

    kaancho Member This Topic's Starter

    Joined:
    Jul 15, 2010
    Messages:
    19
    Likes Received:
    0
    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.
     

    Advertisement
  2. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    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 (Text):
    1.  
    2. URL address = URL.parse("http://yoururlhere.com/yourfile.txt")
    3. URLConnection conn = new URLConnection(address);
    4. InputStream is = conn.getInputStream();
    5. BufferedInputStream bis = new BufferedInputStream(is);
    6. ByteArrayBuffer bab = new ByteArrayBuffer(64);
    7. int current = 0;
    8. while((current = bis.read()) != -1)
    9. {
    10.          bab.append((byte)current);
    11. }
    12.  
    13. FileOutputStream fos = new FileOutputStream(new File(filepath));
    14. fos.write(bab.toByteArray());
    15. fos.close();
    16.  
    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.
     
  3. kaancho

    kaancho Member This Topic's Starter

    Joined:
    Jul 15, 2010
    Messages:
    19
    Likes Received:
    0
    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 (Text):
    1.  
    2.                 String fileName = "filename";
    3.             URL url = new URL("some url");
    4.             File file = new File(fileName);
    5.            
    6.             URLConnection ucon = url.openConnection();
    7.             InputStream is = ucon.getInputStream();
    8.            
    9.             BufferedInputStream bis = new BufferedInputStream(is);
    10.             ByteArrayBuffer baf = new ByteArrayBuffer(50);
    11.            
    12.             int current = 0;
    13.             while ((current = bis.read()) != -1) {
    14.                 baf.append((byte) current);
    15.             }
    16.             FileOutputStream fos = new FileOutputStream(file);
    17.             fos.write(baf.toByteArray());
    18.             fos.close();
    Any idea how to fix this?
     
  4. kaancho

    kaancho Member This Topic's Starter

    Joined:
    Jul 15, 2010
    Messages:
    19
    Likes Received:
    0
    I changed to the

    Code (Text):
    1. BufferedInputStream bis = new BufferedInputStream(is, 8096);
    and i dont get the previously mentioned warning.
    however, i dont seem to get past the

    Code (Text):
    1. FileOutputStream fos = new FileOutputStream(file);
    Is there something I am missing ?
     
  5. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    Are you testing this on an emulator or actual device?
     
  6. kaancho

    kaancho Member This Topic's Starter

    Joined:
    Jul 15, 2010
    Messages:
    19
    Likes Received:
    0
    hi,
    i am testing this on emulator in ubuntu
     
  7. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    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.
     
  8. kaancho

    kaancho Member This Topic's Starter

    Joined:
    Jul 15, 2010
    Messages:
    19
    Likes Received:
    0
    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 (Text):
    1.  
    2. I/ActivityManager(   61): Start proc con.android.readfile for activity con.android.readfile/.ReadFile: pid=316 uid=10023 gids={3003, 1015}
    3. E/AndroidRuntime(  309): ERROR: thread attach failed
    4. D/dalvikvm(  309): LinearAlloc 0x0 used 636716 of 5242880 (12%)
    5. D/ddm-heap(  316): Got feature list request
    6. I/System.out(  316): xxxxxxxxxxxxxxxxxx
    7. E/BACKGROUND_PROC(  316): /test.json
    8.  
    There is another system out that is supposed to be present after the
    Code (Text):
    1. FileOutputStream fos = new FileOutputStream(file);
    but I dont see it.

    any thoughts?
     
  9. kaancho

    kaancho Member This Topic's Starter

    Joined:
    Jul 15, 2010
    Messages:
    19
    Likes Received:
    0
    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.
     
  10. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    Do you have the WRITE_EXTERNAL_STORAGE permission added in your manifest?
     
  11. kaancho

    kaancho Member This Topic's Starter

    Joined:
    Jul 15, 2010
    Messages:
    19
    Likes Received:
    0
    Code (Text):
    1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
     
  12. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    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 (Text):
    1.  
    2. File file = new File(fileName);
    3.  
    Should look like this:
    Code (Text):
    1.  
    2. 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.
    3.  
     

Share This Page

Loading...