我正在尝试将选定的图像从Android设备上传到Azure Blob存储。
但是我的代码失败了:
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
请帮忙! :)
这是我的代码:
import com.microsoft.windowsazure.services.core.storage.*;
import com.microsoft.windowsazure.services.blob.client.*;
public class MainActivity extends Activity {
public static final String storageConnectionString =
"DefaultEndpointsProtocol=http;" +
"AccountName=myName;" +
"AccountKey=YOLO";
Uri currImageURI;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// To open up a gallery browser
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
// currImageURI is the global variable I'm using to hold the content:// URI of the image
currImageURI = data.getData();
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(currImageURI.toString());
//Here starts the code for Azure Storage Blob
try{
// Retrieve storage account from connection-string
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
// Create the blob client
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
// Get a reference to a container
// The container name must be lower case
CloudBlobContainer container = blobClient.getContainerReference("mycontainer");
// Create the container if it does not exist
container.createIfNotExist();
// 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("myimageYdolo.jpg");
File source = new File(currImageURI.toString());
blob.upload(new FileInputStream(source), source.length());
}
catch(Exception e){
}
}
}
}
答案 0 :(得分:0)
帐户密钥对我来说不正确。你可以从Azure Management Portal获得它。导航到存储帐户并查看底部的“管理访问密钥”按钮。您应该看到两个键,您可以使用其中的任何一个键。请参阅分步说明here。