【问题标题】:Android - How to upload image to Azure Storage Blob?Android - 如何将图像上传到 Azure 存储 Blob?
【发布时间】:2016-02-29 21:29:59
【问题描述】:

我正在尝试将所选图像从 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){

                        }
                }
        }
}

【问题讨论】:

    标签: android eclipse image azure-blob-storage


    【解决方案1】:

    帐户密钥在我看来不正确。您可以从Azure Management Portal 获取它。导航到存储帐户并查看底部的“管理访问密钥”按钮。您应该看到两个键,您可以使用其中的任何一个。请参阅分步说明here。

    【讨论】:

    • 我没有显示原始帐户密钥 :) 但是当我创建常规 Java 项目时原始帐户密钥有效。只有在 Android 中才会失败...
    • 您没有包含原始帐户密钥是有道理的。异常消息是什么?
    • 04-09 18:54:00.921: E/AndroidRuntime(7473): 致命异常: main 04-09 18:54:00.921: E/AndroidRuntime(7473): java.lang.NoClassDefFoundError: com.microsoft.windowsazure.services.core.storage.CloudStorageAccount 04-09 18:54:00.921: E/AndroidRuntime(7473): at com.example.uploadtest.MainActivity.onActivityResult(MainActivity.java:63) 04-09 18 :54:00.921: E/AndroidRuntime(7473): 在 android.app.Activity.dispatchActivityResult(Activity.java:5385) 04-09 18:54:00.921: E/AndroidRuntime(7473): 在 android.app.ActivityThread。交付结果(ActivityThread.java:3843)
    • 您是否有旧版本的库位于构建路径中或被引用?
    • 我不这么认为,我从这里下载了 Azure 库:dl.msopentech.com/lib/…
    猜你喜欢
    • 2020-01-25
    • 2018-12-17
    • 1970-01-01
    • 2022-11-03
    • 2021-01-16
    • 1970-01-01
    • 2014-05-22
    • 2020-01-09
    • 2020-06-07
    相关资源
    最近更新 更多