gpt4 book ai didi

android - 使用 asp.net WebService 和 Android 将图像上传到 Azure Blob 存储?

转载 作者:搜寻专家 更新时间:2023-11-01 08:51:28 40 4
gpt4 key购买 nike

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

我制作的 asp.net WebService。

但我在 android 中收到一个橙色错误:“W/System.err(454): SoapFault - faultcode: 'soap:Server' faultstring: 'Server was unable to process request. ---> Object reference not set to一个对象的实例。 faultactor:'null' 详细信息:org.kxml2.kdom.Node@4205f358"

我不确定是我的 Java 代码还是 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";
}

我已经在 Forms .net 中测试了这段代码,它在解析 Base64 字符串并将其转换为 byte[] 时工作正常。所以我不认为这是错误的 WebService 代码..

请帮帮我!

这里是 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:我已授予对 Internet、WRITE_EXTERNAL_STORAGE 和 RECORD_AUDIO 的使用权限

最佳答案

我终于解决了这个问题:D wihu!

在 WebService 中,我必须更改:

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
ConfigurationManager.GetSetting("StorageConnectionString"));

对此(几乎相同):

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.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);
}

就是这样。

所以 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";
}

这会将图像作为 ByteArray 发送到 Windows Azure 存储。

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

如果这有帮助请给我一些分数:D

关于android - 使用 asp.net WebService 和 Android 将图像上传到 Azure Blob 存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22966975/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com