简体   繁体   English

Android-如何将图像上传到Azure存储Blob?

[英]Android - How to upload image to Azure Storage Blob?

I'm trying to upload a selected image from the android device to Azure Blob Storage. 我正在尝试将选定的图像从android设备上传到Azure Blob存储。

But my code fails at: 但是我的代码在以下位置失败:

CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);

Please Help! 请帮忙! :) :)

Here is my code: 这是我的代码:

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){

                        }
                }
        }
}

The account key does not look correct to me. 该帐户密钥对我来说似乎不正确。 You can get it from the Azure Management Portal . 您可以从Azure管理门户获取它。 Navigate to storage account and look at the bottom for a "Manage Access Keys" button. 导航到存储帐户,然后在底部查找“管理访问密钥”按钮。 You should see two keys, any of which you can use. 您应该看到两个键,您可以使用任何一个。 See step by step instructions here . 请参阅此处的分步说明。

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

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