Android OS - 如何跟踪 Azure 上传进度
Android OS - How to track Azure upload progress
我一直在 Android
OS 上使用 Azure
,我设法将我的视频文件 (.mp4) 上传到我已经准备好的容器中。
我首先得到了一个 Shared Access Signature (SAS)
,这为我提供了:
- 临时密钥
- 我要将文件发送到的容器的名称
- 服务器 URI
然后,我开始 AsyncTask
使用 "upload" 将文件发送到容器。
我检查了容器,文件完美上传,没有问题。
我的问题是关于上传的进度。有可能追踪吗?我想要一个上传栏来提供更好的用户体验。
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;
}
谢谢!
您可以拆分文件并使用 Block 发送它的部分,this link 中有一个很好的案例示例,但它使用 C#,因此您应该在 android 库中找到相应的函数参考。
基本上不是将文件作为一个大文件发送给您,而是将其拆分为多个文件(字节)并将其发送到 azure,这样您就可以跟踪已发送到 azure 的字节数的进度
我一直在 Android
OS 上使用 Azure
,我设法将我的视频文件 (.mp4) 上传到我已经准备好的容器中。
我首先得到了一个 Shared Access Signature (SAS)
,这为我提供了:
- 临时密钥
- 我要将文件发送到的容器的名称
- 服务器 URI
然后,我开始 AsyncTask
使用 "upload" 将文件发送到容器。
我检查了容器,文件完美上传,没有问题。
我的问题是关于上传的进度。有可能追踪吗?我想要一个上传栏来提供更好的用户体验。
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;
}
谢谢!
您可以拆分文件并使用 Block 发送它的部分,this link 中有一个很好的案例示例,但它使用 C#,因此您应该在 android 库中找到相应的函数参考。
基本上不是将文件作为一个大文件发送给您,而是将其拆分为多个文件(字节)并将其发送到 azure,这样您就可以跟踪已发送到 azure 的字节数的进度