[英]Using asp.net WebService & Android to upload Image to Azure Blob Storage?
I'm trying to upload a selected image to Azure Blob from my Android device through 我正尝试通过Android设备将所选图像从我的Android设备上传到Azure Blob
a asp.net WebService I've made. 我制作的一个asp.net WebService。
But I get an orange error in android: "W/System.err(454): SoapFault - faultcode: 'soap:Server' faultstring: 'Server was unable to process request. ---> Object reference not set to an instance of an object.' 但是我在android中收到一个橙色错误:“ W / System.err(454):SoapFault-故障代码:'soap:Server'故障字符串:'服务器无法处理请求。--->对象引用未设置为的实例一个东西。' faultactor: 'null' detail: org.kxml2.kdom.Node@4205f358 " faultactor:“ null”详细信息:org.kxml2.kdom.Node@4205f358“
I'm not sure if it's my Java code or WebService witch is wrong... 我不确定这是我的Java代码还是WebService错了...
Here is both codes: 这是两个代码:
WebService: 网络服务:
[WebMethod]
public string UploadFile(string myBase64String, string fileName)
{
byte[] f = Convert.FromBase64String(myBase64String);
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve a reference to a container.
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
// Create the container if it doesn't already exist.
container.CreateIfNotExists();
container.SetPermissions(
new BlobContainerPermissions
{
PublicAccess = BlobContainerPublicAccessType.Blob
});
// Retrieve reference to a blob named "myblob".
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
using (MemoryStream stream = new MemoryStream(f))
{
blockBlob.UploadFromStream(stream);
}
return "OK";
}
I have tested this code in Forms .net, and it works fine when parsing a Base64 string and converting it to byte[]. 我已经在Forms .net中测试了此代码,并且在解析Base64字符串并将其转换为byte []时可以正常工作。 So I don't think it's WebService code that's wrong.. 因此,我认为WebService代码没有错。
Pleas help me! 请帮我!
Here is Java->Android: 这是Java-> Android:
private String TAG = "PGGURU";
Uri currImageURI;
String encodedImage;
@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);
}
byte[] b;
@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();
String ImageUri = getRealPathFromURI(currImageURI);
Bitmap bm = BitmapFactory.decodeFile(ImageUri);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
b = baos.toByteArray();
//encoded image to Base64
encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
//Create instance for AsyncCallWS
AsyncCallWS task = new AsyncCallWS();
task.execute();
}
}
}
public void UploadImage(String image, String imageName) {
//Create request
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//Property which holds input parameters
PropertyInfo PI = new PropertyInfo();
PI.setName("myBase64String");
PI.setValue(image);
PI.setType(String.class);
request.addProperty(PI);
PI=new PropertyInfo();
PI.setName("fileName");
PI.setValue(imageName);
PI.setType(String.class);
request.addProperty(PI);
//Create envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
//Set output SOAP object
envelope.setOutputSoapObject(request);
//Create HTTP call object
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
//Invole web service
androidHttpTransport.call(SOAP_ACTION, envelope);
//Get the response
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
//Assign it to fahren static variable
} catch (Exception e) {
e.printStackTrace();
}
}
private class AsyncCallWS extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
Log.i(TAG, "doInBackground");
UploadImage(encodedImage, "randomName");
return null;
}
@Override
protected void onPostExecute(Void result) {
Log.i(TAG, "onPostExecute");
}
@Override
protected void onPreExecute() {
Log.i(TAG, "onPreExecute");
}
@Override
protected void onProgressUpdate(Void... values) {
Log.i(TAG, "onProgressUpdate");
}
}
PS: I have granted uses-permission to Internet,WRITE_EXTERNAL_STORAGE and RECORD_AUDIO PS:我已授予Internet,WRITE_EXTERNAL_STORAGE和RECORD_AUDIO的使用权限
Finally I solved the problem :D wihu! 终于我解决了问题:D wihu!
In the WebService I had to change: 在WebService中,我必须更改:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
ConfigurationManager.GetSetting("StorageConnectionString"));
to this(almost the same): 对此(几乎相同):
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
and then go to => 'Manage nuget packages' in VS12, and install Windows Azure Storage. 然后转到VS12中的=>“管理nuget程序包”,然后安装Windows Azure存储。
Further more I had to move the variable: byte[] f = Convert.FromBase64String(myBase64String); 我还需要移动变量:byte [] f = Convert.FromBase64String(myBase64String);
outside of the method, like this: 在方法之外,如下所示:
byte[] f;
[WebMethod]
public string UploadFile(string myBase64String, string fileName)
{
f = Convert.FromBase64String(myBase64String);
}
And that was it. 就是这样。
So the WebService look like this: 因此,WebService如下所示:
byte[] f;
[WebMethod]
public string UploadFile(string myBase64String, string fileName)
{
f = Convert.FromBase64String(myBase64String);
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve a reference to a container.
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
// Create the container if it doesn't already exist.
container.CreateIfNotExists();
container.SetPermissions(
new BlobContainerPermissions
{
PublicAccess = BlobContainerPublicAccessType.Blob
});
// Retrieve reference to a blob named "myblob".
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
using (MemoryStream stream = new MemoryStream(f))
{
blockBlob.UploadFromStream(stream);
}
return "OK";
}
This will send the image as a ByteArray to the Windows Azure Storage. 这会将图像作为ByteArray发送到Windows Azure存储。
Next step is to download the file and convert it to a Bitmap image :) 下一步是下载文件并将其转换为位图图像:)
If this was helpfull please give me some points :D 如果这有帮助,请给我几点:D
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.