将 imageview 从 Android 发送到 WCF
Send imageview to WCF from Android
我将图像视图转换为位图,后来转换为 Base64 字符串,之后我通过 post 将其发送到服务器。
我在我的 WCF 中接收流,但保存的文件已损坏,无法看到图像文件。
Android代码:
private void upload() {
BitmapDrawable drawable = (BitmapDrawable) perfilimg.getDrawable();
Bitmap bitmapOrg = drawable.getBitmap();
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 100, bao);
byte[] ba = bao.toByteArray();
ba1 = Base64.encodeBytes(ba);
// Upload image to server
new uploadToServer().execute();
}
public class uploadToServer extends AsyncTask<Void, Void, String> {
private ProgressDialog pd = new ProgressDialog(Perfil.this);
protected void onPreExecute() {
super.onPreExecute();
pd.setMessage("Uploading image...");
pd.show();
}
@Override
protected String doInBackground(Void... params) {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair("base64", ba1));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://dotstudioservices.com/elswitchService/Service1.svc/perfilimagen");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
Log.e("Pruebas", result);
} catch (Exception e) {
Log.v("log_tag", "Error in http connection " + e.toString());
}
return "Success";
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
pd.hide();
pd.dismiss();
}
WCF 代码:
public Estatus FileUpload(Stream stream)
{
byte[] buffer = new byte[50000];
stream.Read(buffer, 0, 50000);
string filePath = "D:\Hosting\9692238\html\promosbc\imagenesElSwitch\profile.jpg";
FileStream f = new FileStream(filePath, FileMode.OpenOrCreate);
f.Write(buffer, 0, buffer.Length);
f.Close();
stream.Close();
return new Estatus { estatus = "success" };
}
我做错了什么?
您应该在写入文件之前尝试在接收端进行 Base64 解码。
为了帮助大家,我将 post 我的答案,我将 Android 代码更改为:
private void upload() {
BitmapDrawable drawable = (BitmapDrawable) perfilimg.getDrawable();
Bitmap bitmapOrg = drawable.getBitmap();
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 100, bao);
byte[] ba = bao.toByteArray();
new uploadToServer().execute();
}
public class uploadToServer extends AsyncTask<Void, Void, String> {
private ProgressDialog pd = new ProgressDialog(Perfil.this);
protected void onPreExecute() {
super.onPreExecute();
pd.setMessage("Uploading image...");
pd.show();
}
@Override
protected String doInBackground(Void... params) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://dotstudioservices.com/elswitchService/Service1.svc/perfilimagen");
httppost.setEntity(new ByteArrayEntity(ba));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
Log.e("Pruebas", result);
} catch (Exception e) {
Log.v("log_tag", "Error in http connection " + e.toString());
}
return "Success";
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
pd.hide();
pd.dismiss();
}
我将图像视图转换为位图,后来转换为 Base64 字符串,之后我通过 post 将其发送到服务器。 我在我的 WCF 中接收流,但保存的文件已损坏,无法看到图像文件。
Android代码:
private void upload() {
BitmapDrawable drawable = (BitmapDrawable) perfilimg.getDrawable();
Bitmap bitmapOrg = drawable.getBitmap();
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 100, bao);
byte[] ba = bao.toByteArray();
ba1 = Base64.encodeBytes(ba);
// Upload image to server
new uploadToServer().execute();
}
public class uploadToServer extends AsyncTask<Void, Void, String> {
private ProgressDialog pd = new ProgressDialog(Perfil.this);
protected void onPreExecute() {
super.onPreExecute();
pd.setMessage("Uploading image...");
pd.show();
}
@Override
protected String doInBackground(Void... params) {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair("base64", ba1));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://dotstudioservices.com/elswitchService/Service1.svc/perfilimagen");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
Log.e("Pruebas", result);
} catch (Exception e) {
Log.v("log_tag", "Error in http connection " + e.toString());
}
return "Success";
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
pd.hide();
pd.dismiss();
}
WCF 代码:
public Estatus FileUpload(Stream stream)
{
byte[] buffer = new byte[50000];
stream.Read(buffer, 0, 50000);
string filePath = "D:\Hosting\9692238\html\promosbc\imagenesElSwitch\profile.jpg";
FileStream f = new FileStream(filePath, FileMode.OpenOrCreate);
f.Write(buffer, 0, buffer.Length);
f.Close();
stream.Close();
return new Estatus { estatus = "success" };
}
我做错了什么?
您应该在写入文件之前尝试在接收端进行 Base64 解码。
为了帮助大家,我将 post 我的答案,我将 Android 代码更改为:
private void upload() {
BitmapDrawable drawable = (BitmapDrawable) perfilimg.getDrawable();
Bitmap bitmapOrg = drawable.getBitmap();
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 100, bao);
byte[] ba = bao.toByteArray();
new uploadToServer().execute();
}
public class uploadToServer extends AsyncTask<Void, Void, String> {
private ProgressDialog pd = new ProgressDialog(Perfil.this);
protected void onPreExecute() {
super.onPreExecute();
pd.setMessage("Uploading image...");
pd.show();
}
@Override
protected String doInBackground(Void... params) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://dotstudioservices.com/elswitchService/Service1.svc/perfilimagen");
httppost.setEntity(new ByteArrayEntity(ba));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
Log.e("Pruebas", result);
} catch (Exception e) {
Log.v("log_tag", "Error in http connection " + e.toString());
}
return "Success";
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
pd.hide();
pd.dismiss();
}