使用asp.net Web服务&;Android将图像上载到Azure Blob存储?

使用asp.net Web服务&;Android将图像上载到Azure Blob存储?,android,asp.net,web-services,azure-storage-blobs,Android,Asp.net,Web Services,Azure Storage Blobs,我正在尝试通过将选定的图像从Android设备上载到Azure Blob 我制作的asp.net Web服务 但我在android中得到一个橙色错误:“W/System.err(454):SoapFault-faultcode:'soap:Server'faultstring:'服务器无法处理请求。-->对象引用未设置为对象的实例。'faultactor:'null'详细信息:org.kxml2.kdom。Node@4205f358 " 我不确定是我的Java代码还是Web服务错了 以下是两个代

我正在尝试通过将选定的图像从Android设备上载到Azure Blob

我制作的asp.net Web服务

但我在android中得到一个橙色错误:“W/System.err(454):SoapFault-faultcode:'soap:Server'faultstring:'服务器无法处理请求。-->对象引用未设置为对象的实例。'faultactor:'null'详细信息:org.kxml2.kdom。Node@4205f358 "

我不确定是我的Java代码还是Web服务错了

以下是两个代码:

网络服务:

    [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";
    }
我已经在Forms.net中测试了这段代码,在解析Base64字符串并将其转换为byte[]时,它运行良好。 所以我不认为是Web服务代码错了

请帮帮我

这里是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");
    }

}
private String TAG=“PGGURU”;
uricurrimageuri;
字符串编码图像;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//打开图库浏览器的步骤
意图=新意图();
intent.setType(“image/*”);
intent.setAction(intent.ACTION\u GET\u CONTENT);
startActivityForResult(Intent.createChooser(Intent,“选择图片”),1);
}
字节[]b;
@凌驾
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
if(resultCode==RESULT\u OK){
if(requestCode==1){
//currImageURI是我用来保存图像的content://URI的全局变量
currImageURI=data.getData();
字符串ImageUri=getRealPathFromURI(currImageURI);
位图bm=BitmapFactory.decodeFile(ImageUri);
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG,100,baos);//bm是位图对象
b=baos.toByteArray();
//将图像编码到Base64
encodedImage=Base64.encodeToString(b,Base64.DEFAULT);
//为AsyncCallWS创建实例
AsyncCallWS任务=新建AsyncCallWS();
task.execute();
}
}
}
public void UploadImage(字符串图像、字符串图像名称){
//创建请求
SoapObject请求=新的SoapObject(名称空间、方法名称);
//保存输入参数的属性
PropertyInfo PI=新的PropertyInfo();
PI.setName(“myBase64String”);
PI.设定值(图像);
PI.setType(String.class);
请求。添加属性(PI);
PI=新属性info();
PI.setName(“文件名”);
PI.setValue(imageName);
PI.setType(String.class);
请求。添加属性(PI);
//创建信封
SoapSerializationEnvelope=新的SoapSerializationEnvelope(
第11版);
envelope.dotNet=true;
//设置输出SOAP对象
envelope.setOutputSoapObject(请求);
//创建HTTP调用对象
HttpTransportSE androidHttpTransport=新的HttpTransportSE(URL);
试一试{
//发票web服务
调用(SOAP_操作,信封);
//得到回应
SoapPrimitive响应=(SoapPrimitive)信封.getResponse();
//将其分配给fahren静态变量
}捕获(例外e){
e、 printStackTrace();
}
}
私有类AsyncCallWS扩展了AsyncTask{
@凌驾
受保护的Void doInBackground(字符串…参数){
Log.i(标签“doInBackground”);
上传图像(encodedImage,“randomName”);
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
Log.i(标记“onPostExecute”);
}
@凌驾
受保护的void onPreExecute(){
Log.i(标记“onPreExecute”);
}
@凌驾
受保护的void onProgressUpdate(void…值){
Log.i(标记“onProgressUpdate”);
}
}

PS:我已经授权使用互联网,写外部存储器和录制音频,最后我解决了这个问题:D wihu

在Web服务中,我必须更改:

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
ConfigurationManager.GetSetting("StorageConnectionString"));
对此(几乎相同):

然后转到VS12中的=>“管理nuget软件包”,并安装Windows Azure存储

此外,我还必须移动变量:byte[]f=Convert.FromBase64String(myBase64String)

在方法之外,如下所示:

    byte[] f;
    [WebMethod]
    public string UploadFile(string myBase64String, string fileName)
    {
         f = Convert.FromBase64String(myBase64String);
    }
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";
    }
就这样

因此,Web服务如下所示:

    byte[] f;
    [WebMethod]
    public string UploadFile(string myBase64String, string fileName)
    {
         f = Convert.FromBase64String(myBase64String);
    }
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";
    }
这会将映像作为ByteArray发送到Windows Azure存储

下一步是下载文件并将其转换为位图图像:)

如果这是有益的,请给我一些分数:D