我正在使用Azure移动后端的Android示例存储应用程序。我很难让它上班。我的模拟器根本无法使用移动服务,所以我没有日志等帮助。没有构建错误,应用运行正常 - 只是没有上传blob。我很确定我确切知道它停止运作的确切位置。通过选择图像和创建sas url它可以正常工作,但它实际上并没有上传。容器早先设置好了......
这是代码,它似乎在轰炸,下面是插入服务器脚本。
任何帮助都非常感谢....
相关客户代码:
class ImageUploaderTask extends AsyncTask<Void, Void, Boolean> {
private String mUrl;
public ImageUploaderTask(String url) {
mUrl = url;
Toast.makeText(getApplicationContext(), "in imageuploader",
Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), url,
Toast.LENGTH_LONG).show();
// both these toasts showing up and there is proper looking url so seem fine up to
// here - to get here have to get the sas url, access container, etc.
}
@Override
protected Boolean doInBackground(Void... params) {
try {
Toast.makeText(getApplicationContext(), "in the TRY ",
Toast.LENGTH_LONG).show();
// This toast not showing up so it seems like not even in this loop...why???
//Get the image data
Cursor cursor = getContentResolver().query(mImageUri, null,null, null, null);
cursor.moveToFirst();
int index = cursor.getColumnIndex`enter code here `enter code here`
(MediaStore.Images.ImageColumns.DATA);
String absoluteFilePath = cursor.getString(index);
FileInputStream fis = new FileInputStream(absoluteFilePath);
int bytesRead = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
while ((bytesRead = fis.read(b)) != -1) {
bos.write(b, 0, bytesRead);
}
byte[] bytes = bos.toByteArray();
// Post our image data (byte array) to the server
URL url = new URL(mUrl.replace("\"", ""));
HttpURLConnection urlConnection = (HttpURLConnection)
url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("PUT");
urlConnection.addRequestProperty("Content-Type", "image/jpeg");
urlConnection.setRequestProperty("Content-Length", ""+ bytes.length);
// Write image data to server
DataOutputStream wr = new DataOutputStream
(urlConnection.getOutputStream());
wr.write(bytes);
wr.flush();
wr.close();
int response = urlConnection.getResponseCode();
//If we successfully uploaded, return true
if (response == 201
&& urlConnection.getResponseMessage().equals
("Created"))
{
Toast.makeText(getApplicationContext(), "output successful",
Toast.LENGTH_LONG).show();
return true;
}
} catch (Exception ex) {
Log.e(TAG, ex.getMessage());
}
return false;
}
Server Insert Script
var azure = require('azure');
var qs = require('querystring');
function insert(item, user, request) {
var accountName = 'private';
var accountKey = '+private';
var host = accountName + '.blob.core.windows.net';
var blobService = azure.createBlobService(accountName, accountKey, host);
var sharedAccessPolicy = {
AccessPolicy: {
Permissions: 'rw', //Read and Write permissions
Expiry: minutesFromNow(5)
}
};
var sasUrl = blobService.generateSharedAccessSignature(request.parameters.containerName,
request.parameters.blobName, sharedAccessPolicy);
var sasQueryString = { 'sasUrl' : sasUrl.baseUrl + sasUrl.path + '?' + qs.stringify
(sasUrl.queryString) };
request.respond(200, sasQueryString);
}
function minutesFromNow(minutes) {
var date = new Date()
date.setMinutes(date.getMinutes() + minutes);
return date;
}