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