我一直试图将azure blob存储与我的Android应用程序集成,但我一直遇到麻烦。我试图将文本文件上传到服务器,但是我发生了IOException错误以及以下日志:
void uploadFile(String filePath, String name){
Log.e("TopMeme","upload beginning for "+name+" @ "+filePath);
try
{
// Retrieve storage account from connection-string.
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.getContainerReference("memecontainer");
// Create or overwrite the "myimage.jpg" blob with contents from a local file.
CloudBlockBlob blob = container.getBlockBlobReference(name);
File source = new File(filePath);
Log.e("TopMeme","File size: "+source.length());
FileInputStream fileInputStream = new FileInputStream(source);
Log.e("TopMeme","available: "+fileInputStream.available());
blob.upload(fileInputStream, source.length());
Log.e("TopMeme","upload function completed");
}
catch (Exception e)
{
// Output the stack trace.
e.printStackTrace();
Log.e("TopMeme","upload failed: "+e);
}
}
这是我的功能,即调用它。我可以确认文件路径存在:
{{1}}
答案 0 :(得分:2)
事实证明,在Azure中你不能在你的主要android线程上做任何网络连接,所以你必须在另一个线程上做。这是我的新功能:
void uploadFile(final String filePath,final String name){
Log.e("TopMeme","upload beginning for "+name+" @ "+filePath);
//cant perform network tasks on main thread
AsyncTask<Void,Void,Void> task = new AsyncTask<Void,Void,Void>(){
@Override
protected Void doInBackground(Void... params) {
try
{
// Retrieve storage account from connection-string.
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.getContainerReference("memecontainer");
/*
//create blob if it doesn't exist - hopefully resolves bugs
container.createIfNotExists();
// Create a permissions object.
BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
// Include public access in the permissions object.
containerPermissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);
// Set the permissions on the container.
container.uploadPermissions(containerPermissions);
*/
// Create or overwrite the "myimage.jpg" blob with contents from a local file.
CloudBlockBlob blob = container.getBlockBlobReference(name);
File source = new File(filePath);
Log.e("TopMeme","File size: "+source.length());
FileInputStream fileInputStream = new FileInputStream(source);
Log.e("TopMeme","available: "+fileInputStream.available());
blob.upload(fileInputStream, source.length());
Log.e("TopMeme","upload function completed");
}
catch (Exception e)
{
// Output the stack trace.
e.printStackTrace();
Log.e("TopMeme","upload failed: "+e);
}
return null;
}
};
task.execute();
}