我的目标是从用户那里获取图像并调整其大小,然后将其上传到 azure 移动应用程序简易表,但我遇到了异常
My Goal is to take Image from user and resize it and then upload it to azure mobile app easy tables but I am geting exception
我正在研究 visual stdio xamarin.android 我的要求是从图库中获取图像并在图像视图中显示,然后将其上传到 azure mobile easy table 我的代码在下面请帮助在哪里我的错误
我的客户数据 class 是:
class ClientData
{
public string Id { get; set; }
[JsonProperty(PropertyName = "UserName")]
public string userName { set; get; }
[JsonProperty(PropertyName = "Passward")]
public string passward { set; get; }
[JsonProperty(PropertyName = "Phone")]
public string phone { set; get; }
[JsonProperty(PropertyName = "City")]
public string city { set; get; }
[JsonProperty(PropertyName = "Work")]
public string work { set; get; }
[JsonProperty(PropertyName = "ProfilePic")]
public string profile { set; get; }
}
我的 Activity 代码是:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;
using System.IO;
using Android.Graphics;
using Microsoft.WindowsAzure.MobileServices;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.MobileServices.Sync;
using Microsoft.WindowsAzure.MobileServices.SQLiteStore;
using Android.Util;
namespace CarryARideAnyWere
{
[Activity(Label = "SignUpActivity", Theme = "@style/MyAppTheme")]
public class SignUpActivity : Activity
{
AutoCompleteTextView Cities;
EditText userName;
EditText passward;
EditText retypePassward;
EditText phone;
EditText work;
Button register;
ImageView profilePic;
Bitmap bit;
byte[] b;
private MobileServiceClient client;
private IMobileServiceSyncTable<ClientData> clientData;
const string LocalDBFileName = "ClientDataFile";
const string ApplicationURL = "http://carryarideanywhere.azurewebsites.net/";
protected override async void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.SignUp);
//perform plateform related initialization
CurrentPlatform.Init();
//to get City of user
Cities = FindViewById<AutoCompleteTextView>(Resource.Id.City);
//geting Layout widgets
userName = FindViewById<EditText>(Resource.Id.userName);
passward = FindViewById<EditText>(Resource.Id.passward);
retypePassward = FindViewById<EditText>(Resource.Id.reenterpass);
phone = FindViewById<EditText>(Resource.Id.PhoneNumber);
work = FindViewById<EditText>(Resource.Id.work);
profilePic = FindViewById<ImageView>(Resource.Id.profilepic);
register = FindViewById<Button>(Resource.Id.Register);
//Geting Array of Cities
String[] citiesofPak = Resources.GetStringArray(Resource.Array.cities);
Cities.Adapter = new ArrayAdapter<string>(this, Resource.Layout.CitiesView, citiesofPak);
//Creating client object
client = new MobileServiceClient(ApplicationURL);
//Initializing LocalStore
await InitLocalStoreAsync();
clientData = client.GetSyncTable<ClientData>();
//Defining Click Event of Register button
register.Click += OnRegisterClick;
//registering click event of image view
profilePic.Click -= OnProfilePicClick;
profilePic.Click += OnProfilePicClick;
b = new byte[] { 12, 12, 12, 12 };
}
private void OnRegisterClick(object sender, EventArgs e)
{
if (userName.Text == string.Empty | passward.Text == string.Empty | retypePassward.Text == string.Empty | phone.Text == string.Empty | Cities.Text == string.Empty | work.Text == string.Empty)
{
CreateDialog.CreateAndShowDialog("Please fill all the Boxes", "Error", this);
return;
}
if (passward.Text != retypePassward.Text)
{
CreateDialog.CreateAndShowDialog("passward Does not match", "Error", this);
return;
}
AddData();
Intent intent = new Intent(this, typeof(LogInActivity));
intent.PutExtra(Constants.Start, true);
// StartActivity(intent);
// this.Finish();
}
private async Task InitLocalStoreAsync()
{
var store = new MobileServiceSQLiteStore(LocalDBFileName);
store.DefineTable<ClientData>();
await client.SyncContext.InitializeAsync(store);
}
private async Task PushData()
{
try
{
await client.SyncContext.PushAsync();
IMobileServiceTableQuery<ClientData> query = clientData
.Where(u => u.phone == phone.Text);
await clientData.PullAsync(null, query);
}
catch (Java.Net.MalformedURLException)
{
CreateDialog.CreateAndShowDialog(new Exception("There was error creating MobileService verify URL"), "Error", this);
}
catch (Exception e)
{
CreateDialog.CreateAndShowDialog(e, "Error", this);
}
}
public async void AddData()
{
if (client == null)
{
return;
}
var Data = new ClientData { userName = userName.Text, passward = passward.Text, phone = phone.Text, city = Cities.Text, work = work.Text,profile=convert(bit)};
try
{
CreateDialog.CreateAndShowDialog(convert(bit).Length.ToString(), "bytearrylength", this);
await clientData.InsertAsync(Data);
await PushData();
}
catch (Exception e)
{
CreateDialog.CreateAndShowDialog(e, "Error in datauploading", this);
}
}
private void OnProfilePicClick(object sender, EventArgs e)
{
Intent intent = new Intent();
intent.SetType("image/*");
intent.SetAction(Intent.ActionGetContent);
StartActivityForResult(Intent.CreateChooser(intent, "Select a pic"), 0);
}
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (resultCode == Result.Ok)
{
bit = calculateRequiredimentions(data.Data, 150, 150);
Stream stream = ContentResolver.OpenInputStream(data.Data);
bit = BitmapFactory.DecodeStream(stream);
profilePic.SetImageBitmap(bit);
}
}
private string convert(Bitmap bitmap)
{
byte[] bitmapData;
string ImageEncoding;
using (var stream = new MemoryStream())
{
bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
bitmapData = stream.ToArray();
ImageEncoding=Base64.EncodeToString(bitmapData,Base64Flags.Default);
CreateDialog.CreateAndShowDialog(bitmapData.Length.ToString(), "bytearrylength", this);
}
return ImageEncoding;
}
private Bitmap calculateRequiredimentions(Android.Net.Uri data,int requestedHeight,int requestedWidth)
{
Stream stream = ContentResolver.OpenInputStream(data);
BitmapFactory.Options options = new BitmapFactory.Options();
options.InJustDecodeBounds = true;
BitmapFactory.DecodeStream(stream);
options.InSampleSize = calcualteintvalue(options, requestedHeight, requestedWidth);
stream = ContentResolver.OpenInputStream(data);
options.InJustDecodeBounds = false;
Bitmap map = BitmapFactory.DecodeStream(stream,null,options);
return map;
}
private int calcualteintvalue(BitmapFactory.Options options,int requestedHeight,int requestedWidth)
{
int height = options.OutHeight;
int width = options.OutWidth;
int returendValue = 1;
if (height > requestedHeight || width > requestedWidth)
{
int halfheight = height / 2;
int halfwidth = width / 2;
while ((halfheight / returendValue) > requestedHeight && (halfwidth / returendValue) > requestedWidth)
{
returendValue *= 2;
}
}
return returendValue;
}
}
}
一些明显的问题:
- 您的连接是 http - 它需要是 https。
- 您在 PullAsync() 中指定了一个查询,但没有查询名称,因此没有增量同步
- 您没有包括用于增量同步和冲突解决的 updatedAt、createdAt 和版本
- 您正在将二进制数据存储在字符串中 - 您不能那样做
- 您假设您的数据小于 255 字节(简单 tables 中字符串的最大大小)
可能还有其他的,但是你没有发布任何异常数据,也没有查看后台日志,所以无从知晓。
看看 http://aka.ms/zumobook 上的书,尤其是第 3 章和第 4 章。第 3 章介绍 table 数据同步,第 4 章介绍自定义 API 和文件操作。
我正在研究 visual stdio xamarin.android 我的要求是从图库中获取图像并在图像视图中显示,然后将其上传到 azure mobile easy table 我的代码在下面请帮助在哪里我的错误
我的客户数据 class 是:
class ClientData
{
public string Id { get; set; }
[JsonProperty(PropertyName = "UserName")]
public string userName { set; get; }
[JsonProperty(PropertyName = "Passward")]
public string passward { set; get; }
[JsonProperty(PropertyName = "Phone")]
public string phone { set; get; }
[JsonProperty(PropertyName = "City")]
public string city { set; get; }
[JsonProperty(PropertyName = "Work")]
public string work { set; get; }
[JsonProperty(PropertyName = "ProfilePic")]
public string profile { set; get; }
}
我的 Activity 代码是:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;
using System.IO;
using Android.Graphics;
using Microsoft.WindowsAzure.MobileServices;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.MobileServices.Sync;
using Microsoft.WindowsAzure.MobileServices.SQLiteStore;
using Android.Util;
namespace CarryARideAnyWere
{
[Activity(Label = "SignUpActivity", Theme = "@style/MyAppTheme")]
public class SignUpActivity : Activity
{
AutoCompleteTextView Cities;
EditText userName;
EditText passward;
EditText retypePassward;
EditText phone;
EditText work;
Button register;
ImageView profilePic;
Bitmap bit;
byte[] b;
private MobileServiceClient client;
private IMobileServiceSyncTable<ClientData> clientData;
const string LocalDBFileName = "ClientDataFile";
const string ApplicationURL = "http://carryarideanywhere.azurewebsites.net/";
protected override async void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.SignUp);
//perform plateform related initialization
CurrentPlatform.Init();
//to get City of user
Cities = FindViewById<AutoCompleteTextView>(Resource.Id.City);
//geting Layout widgets
userName = FindViewById<EditText>(Resource.Id.userName);
passward = FindViewById<EditText>(Resource.Id.passward);
retypePassward = FindViewById<EditText>(Resource.Id.reenterpass);
phone = FindViewById<EditText>(Resource.Id.PhoneNumber);
work = FindViewById<EditText>(Resource.Id.work);
profilePic = FindViewById<ImageView>(Resource.Id.profilepic);
register = FindViewById<Button>(Resource.Id.Register);
//Geting Array of Cities
String[] citiesofPak = Resources.GetStringArray(Resource.Array.cities);
Cities.Adapter = new ArrayAdapter<string>(this, Resource.Layout.CitiesView, citiesofPak);
//Creating client object
client = new MobileServiceClient(ApplicationURL);
//Initializing LocalStore
await InitLocalStoreAsync();
clientData = client.GetSyncTable<ClientData>();
//Defining Click Event of Register button
register.Click += OnRegisterClick;
//registering click event of image view
profilePic.Click -= OnProfilePicClick;
profilePic.Click += OnProfilePicClick;
b = new byte[] { 12, 12, 12, 12 };
}
private void OnRegisterClick(object sender, EventArgs e)
{
if (userName.Text == string.Empty | passward.Text == string.Empty | retypePassward.Text == string.Empty | phone.Text == string.Empty | Cities.Text == string.Empty | work.Text == string.Empty)
{
CreateDialog.CreateAndShowDialog("Please fill all the Boxes", "Error", this);
return;
}
if (passward.Text != retypePassward.Text)
{
CreateDialog.CreateAndShowDialog("passward Does not match", "Error", this);
return;
}
AddData();
Intent intent = new Intent(this, typeof(LogInActivity));
intent.PutExtra(Constants.Start, true);
// StartActivity(intent);
// this.Finish();
}
private async Task InitLocalStoreAsync()
{
var store = new MobileServiceSQLiteStore(LocalDBFileName);
store.DefineTable<ClientData>();
await client.SyncContext.InitializeAsync(store);
}
private async Task PushData()
{
try
{
await client.SyncContext.PushAsync();
IMobileServiceTableQuery<ClientData> query = clientData
.Where(u => u.phone == phone.Text);
await clientData.PullAsync(null, query);
}
catch (Java.Net.MalformedURLException)
{
CreateDialog.CreateAndShowDialog(new Exception("There was error creating MobileService verify URL"), "Error", this);
}
catch (Exception e)
{
CreateDialog.CreateAndShowDialog(e, "Error", this);
}
}
public async void AddData()
{
if (client == null)
{
return;
}
var Data = new ClientData { userName = userName.Text, passward = passward.Text, phone = phone.Text, city = Cities.Text, work = work.Text,profile=convert(bit)};
try
{
CreateDialog.CreateAndShowDialog(convert(bit).Length.ToString(), "bytearrylength", this);
await clientData.InsertAsync(Data);
await PushData();
}
catch (Exception e)
{
CreateDialog.CreateAndShowDialog(e, "Error in datauploading", this);
}
}
private void OnProfilePicClick(object sender, EventArgs e)
{
Intent intent = new Intent();
intent.SetType("image/*");
intent.SetAction(Intent.ActionGetContent);
StartActivityForResult(Intent.CreateChooser(intent, "Select a pic"), 0);
}
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (resultCode == Result.Ok)
{
bit = calculateRequiredimentions(data.Data, 150, 150);
Stream stream = ContentResolver.OpenInputStream(data.Data);
bit = BitmapFactory.DecodeStream(stream);
profilePic.SetImageBitmap(bit);
}
}
private string convert(Bitmap bitmap)
{
byte[] bitmapData;
string ImageEncoding;
using (var stream = new MemoryStream())
{
bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
bitmapData = stream.ToArray();
ImageEncoding=Base64.EncodeToString(bitmapData,Base64Flags.Default);
CreateDialog.CreateAndShowDialog(bitmapData.Length.ToString(), "bytearrylength", this);
}
return ImageEncoding;
}
private Bitmap calculateRequiredimentions(Android.Net.Uri data,int requestedHeight,int requestedWidth)
{
Stream stream = ContentResolver.OpenInputStream(data);
BitmapFactory.Options options = new BitmapFactory.Options();
options.InJustDecodeBounds = true;
BitmapFactory.DecodeStream(stream);
options.InSampleSize = calcualteintvalue(options, requestedHeight, requestedWidth);
stream = ContentResolver.OpenInputStream(data);
options.InJustDecodeBounds = false;
Bitmap map = BitmapFactory.DecodeStream(stream,null,options);
return map;
}
private int calcualteintvalue(BitmapFactory.Options options,int requestedHeight,int requestedWidth)
{
int height = options.OutHeight;
int width = options.OutWidth;
int returendValue = 1;
if (height > requestedHeight || width > requestedWidth)
{
int halfheight = height / 2;
int halfwidth = width / 2;
while ((halfheight / returendValue) > requestedHeight && (halfwidth / returendValue) > requestedWidth)
{
returendValue *= 2;
}
}
return returendValue;
}
}
}
一些明显的问题:
- 您的连接是 http - 它需要是 https。
- 您在 PullAsync() 中指定了一个查询,但没有查询名称,因此没有增量同步
- 您没有包括用于增量同步和冲突解决的 updatedAt、createdAt 和版本
- 您正在将二进制数据存储在字符串中 - 您不能那样做
- 您假设您的数据小于 255 字节(简单 tables 中字符串的最大大小)
可能还有其他的,但是你没有发布任何异常数据,也没有查看后台日志,所以无从知晓。
看看 http://aka.ms/zumobook 上的书,尤其是第 3 章和第 4 章。第 3 章介绍 table 数据同步,第 4 章介绍自定义 API 和文件操作。