How to download a file stored in Backendless with your Android app | Backend as a Service Platform

Message:

Subscribe rss
Blog categories
All | Backendless features
How to download a file stored in Backendless with your Android app
June 11, 2018 by Anton Govorushkin

Since Backendless does not have native APIs to download files, today we’ll talk about how to implement this function in your Android application. In order to do that, we’ll need to combine Backendless file listing API and android.app.DownloadManager. There are several alternatives to this approach, but the selected one requires less code to write and has a well-thought structure.

Once a developer uploads files to the Backendless Files system, each file gets a public URL which can either be obtained using the Backendless Console or calculated using the following URL scheme:

publicURL 1024x472 - How to download a file stored in Backendless with your Android appThis public URI is the full path to the file in your Backendless file system. Directory listing API returns a list of the  FileInfo objects representing the files located in the directory, where each element in the collection contains the following properties:

  • name  – the name of the file or directory without any path information
  • public URL  – absolute URL of the file or directory
  • URL  – relative URL of the file or directory starting from the root of the file storage
  • created on  – a timestamp indicating when the file or directory was created

In this guide, we’ll be dealing with listing and downloading all the files with the  .html  extension in the /web  folder located in the root directory of your Backendless file system.

As for the Android app itself, we’ll need to:

  1. Define the  INTERNET and WRITE_EXTERNAL_STORAGE  permissions in Android manifest:
  2. Create a method to list all files in the directory:

    In this code sample, allFiles is a variable, which stores the results of the listing operation, and filesList is a TextView class where we show all the existing files.

    When the listing process is finished, we can start the download.

  3. Create a method which gets the public URIs of each file in a directory and will download these files in series:

    That’s all we need to make it work on Android devices with SDK < 23. On devices with SDK 23+, we’ll have application users to grant the permission to use their device storage. A method to check the device SDK and to ask for permission (if necessary) may look like this (in our code sample it is called before the download starts in Step 3):

We need to call this method in download method of your download activity  class. The result of the user choice should be handled in a separate callback, the same callback the download process should be restored:

Where downloadFiles is a button to start the download.

 The whole test app interface will look like this:

mainActivity - How to download a file stored in Backendless with your Android app

Selection 154 - How to download a file stored in Backendless with your Android app

Once you click the ALLOW button, the download process will be started.

Selection 155 - How to download a file stored in Backendless with your Android app

That’s it! The archive with this application can be found here: https://drive.google.com/open?id=1A4Mx3UEWK_ckgaSjaw0TWU5OvVq4ocLY

Don’t forget to add your Backendless AppId and Android API key in the  Defaults class.

Thanks for reading this post, hope you like it! 

Share this post
Tweet about this on TwitterShare on FacebookGoogle+