Search code examples
androidazureblobprogress

Android OS - How to track Azure upload progress


I've been working with Azure on the Android OS and I managed to upload my video file (.mp4) to a Container I had already prepared for it.

I did this by getting a Shared Access Signature (SAS) first, which provided me with:

  • a temporary key
  • the name of the container to where I want to send the files
  • the server URI

Then, I started an AsyncTask to send the file to the container using the "upload".

I checked the container, and the file gets uploaded perfectly, no problems on that end.

My question is regarding the progress of the upload. Is it possible to track it? I would like to have an upload bar to give a better UX.

P.S - I'm using the Azure Mobile SDK

Here's my code:

private void uploadFile(String filename){

        mFileTransferInProgress = true;
        try {
            Log.d("Funky Stuff", "Blob Azure Config");
            final String gFilename = filename;

            File file = new File(filename); // File path

            String blobUri = blobServerURL + sharedAccessSignature.replaceAll("\"", "");

            StorageUri storage = new StorageUri(URI.create(blobUri));

            CloudBlobClient blobCLient = new CloudBlobClient(storage);

            //Container name here
            CloudBlobContainer container = blobCLient.getContainerReference(blobContainer);

            blob = container.getBlockBlobReference(file.getName());

            //fileToByteConverter is a method to convert files to a byte[]
            byte[] buffer = fileToByteConverter(file);

            ByteArrayInputStream  inputStream = new ByteArrayInputStream(buffer);

            if (blob != null) {
                new UploadFileToAzure().execute(inputStream);
            }

        } catch (StorageException e) {
            Log.d("Funky Stuff", "StorageException: " + e.toString());
            e.printStackTrace();
        } catch (IOException e) {
            Log.d("Funky Stuff", "IOException: " + e.toString());
            e.printStackTrace();
        } catch (Exception e) {
            Log.d("Funky Stuff", "Exception: " + e.toString());
            e.printStackTrace();
        }

        mFileTransferInProgress = false;
        //TODO: Missing ProgressChanged method from AWS

    }



 private class UploadFileToAzure extends 
AsyncTask <ByteArrayInputStream, Void, Void> 

{


        @Override
        protected Void doInBackground(ByteArrayInputStream... params) {
            try {
                Log.d("Funky Stuff", "Entered UploadFileToAzure Async" + uploadEvent.mFilename);

                //Method to upload, takes an InputStream and a size
                blob.upload(params[0], params[0].available());
                params[0].close();

            } catch (StorageException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

Thanks!


Solution

  • You can split your file and send its part using Block, there is a good example of your case in this link but it used C# so you should find the corresponding function in the android library reference.

    Basically instead of sending you file as one big file, you split it to multiple files (bytes) and send it to azure so you can track the progress on how many bytes that already sent to azure