我将imageview转换为位图,然后转换为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" };
}
我做错了什么?
答案 0 :(得分:0)
在写入文件之前,您应该尝试在接收端进行Base64解码。
答案 1 :(得分:0)
为了帮助大家,我将发布我的答案,我更改了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();
}