0
我正在尝试将所选图像从android设备上传到Azure Blob存储。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){
}
}
}
}
我没有显示原始帐户密钥:) 但是,原始帐户密钥在我创建常规Java项目时起作用。只有在Android中它失败... – Christer
有意义,您没有包含原始帐户密钥。什么是异常消息? –
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):\t at com.example.uploadtest.MainActivity.onActivityResult(MainActivity.java:63) 04-09 18:54: 00.921:E/AndroidRuntime(7473):\t at android.app.Activity.dispatchActivityResult(Activity.java:5385) 04-09 18:54:00.921:E/AndroidRuntime(7473):\t at android.app.ActivityThread。 deliverResults(ActivityThread.java:3843) – Christer