简体   繁体   中英

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.

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 . 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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