简体   繁体   English

Android OS-如何跟踪Azure上传进度

[英]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. 我一直在Android操作系统上使用Azure ,并且设法将视频文件(.mp4)上传到已经为此准备的容器中。

I did this by getting a Shared Access Signature (SAS) first, which provided me with: 为此,我首先获得了Shared Access Signature (SAS) ,从而为我提供了:

  • a temporary key 临时密钥
  • the name of the container to where I want to send the files 要将文件发送到的容器的名称
  • the server URI 服务器URI

Then, I started an AsyncTask to send the file to the container using the "upload". 然后,我启动了一个AsyncTask ,使用“上传”将文件发送到容器。

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. 我想要一个上传栏,以提供更好的用户体验。

PS - I'm using the Azure Mobile SDK PS-我正在使用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! 谢谢!

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. 您可以使用Block拆分文件并发送文件,在此链接中有一个很好的例子,但是它使用了C#,因此您应该在android库参考中找到相应的函数。

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 基本上,不是将文件作为一个大文件发送,而是将其拆分为多个文件(字节)并将其发送到天蓝色,以便您可以跟踪已发送到天蓝色的字节数的进度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM