从Android向WCF发送imageview
我将imageview转换为位图,然后转换为Base64字符串,然后通过post将其发送到服务器。 我正在WCF中接收流,但保存的文件已损坏,图像文件无法查看 Android代码:从Android向WCF发送imageview,android,wcf,post,imageview,Android,Wcf,Post,Imageview,我将imageview转换为位图,然后转换为Base64字符串,然后通过post将其发送到服务器。 我正在WCF中接收流,但保存的文件已损坏,图像文件无法查看 Android代码: private void upload() { BitmapDrawable drawable = (BitmapDrawable) perfilimg.getDrawable(); Bitmap bitmapOrg = drawable.getBitmap(); ByteArrayO
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();
}
我做错了什么?在写入文件之前,您应该尝试在接收端执行Base64解码。为了帮助所有人,我将发布我的答案,我更改了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();
}
private void upload(){
BitmapDrawable drawable=(BitmapDrawable)perfilimg.getDrawable();
位图bitmapOrg=drawable.getBitmap();
ByteArrayOutputStream bao=新建ByteArrayOutputStream();
压缩(Bitmap.CompressFormat.JPEG,100,bao);
字节[]ba=bao.toByteArray();
新建uploadToServer().execute();
}
公共类uploadToServer扩展异步任务{
private ProgressDialog pd=新建ProgressDialog(Perfil.this);
受保护的void onPreExecute(){
super.onPreExecute();
设置消息(“上传图像…”);
pd.show();
}
@凌驾
受保护字符串doInBackground(无效…参数){
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://dotstudioservices.com/elswitchService/Service1.svc/perfilimagen");
httppost.setEntity(新的ByteArrayEntity(ba));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
字符串结果=EntityUtils.toString(实体);
Log.e(“Pruebas”,结果);
}捕获(例外e){
Log.v(“Log_标记”,“http连接错误”+e.toString());
}
返回“成功”;
}
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
pd.hide();
pd.解散();
}
我不知道如何从流中执行此操作,然后再从包含base64内容的文件中执行此操作。您不是在进行文件上载。您所做的是发送一个普通的键值文本对。因此,我甚至想知道是否将某些内容保存到文件中。请使用写字板查看保存的内容。我有这样的内容:base64=%2F9j%2F4AAqskzjrgabaqaaaqbaad%2f2wbdaaamcagmmcagmdawmeawebqgfbqeqeqobwyidaomdaskcwsndhiqdq4rdgslebyqe…好的。确实是我已经告诉过你的key=值对。从中删除base64=就得到了base64编码的图像。现在你可以对它进行解码了。但是像%2F这样的字符串中有不允许的字符。这看起来像是一个url编码的字符。你是url编码的吗在发布之前对字符串进行编码?或者您可以在解码base64之前对该字符串进行url解码。在代码中,我有:httppost.setEntity(新的UrlEncodedFormEntity(nameValuePairs));我想我应该发送base64编码而不发送nameValuePairs…我想我需要发送一个列表…我不能只发送一个字符串
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();
}