Android操作系统 - 如何跟踪Azure上传进度

时间:2015-12-04 12:22:44

标签: android azure blob progress

我一直在Azure操作系统上与Android合作,我设法将我的视频文件(.mp4)上传到我已经准备好的容器中。

我首先获得Shared Access Signature (SAS),这为我提供了:

  • 临时密钥
  • 我要发送文件的容器的名称
  • 服务器URI

然后,我开始AsyncTask使用"上传"将文件发送到容器。

我检查了容器,文件上传完美,没有问题。

我的问题是关于上传的进度。有可能跟踪它吗?我想有一个上传栏,以提供更好的用户体验。

P.S - 我正在使用Azure Mobile SDK

这是我的代码:

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;
        }

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以拆分文件并使用Block发送其部分,在this link中有一个很好的例子,但它使用了C#,因此你应该在android库参考中找到相应的函数。

基本上不是将文件作为一个大文件发送,而是将其拆分为多个文件(字节)并将其发送到azure,这样您就可以跟踪已经发送到azure的字节数的进度

相关问题