我正在尝试通过以下方式将选定的图像从我的 Android 设备上传到 Azure Blob
我制作的 asp.net WebService。
但我在 android 中收到橙色错误:“W/System.err(454): SoapFault - 故障代码:'soap:Server' 故障字符串:'服务器无法处理请求。 ---> 对象引用未设置为实例一个东西。'故障因素:“空”详细信息: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[] 时它工作得很好。
所以我不认为 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 使用权限