【问题标题】:Send imageview to WCF from Android从 Android 发送 imageview 到 WCF
【发布时间】:2015-03-11 15:41:57
【问题描述】:

我将图像视图转换为位图,然后转换为 Base64 字符串,之后我通过邮寄将其发送到服务器。 我在 WCF 中接收流,但保存的文件已损坏,无法查看图像文件。

安卓代码:

   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=%2F9j%2F4AAQSkZJRgABAQAAAQABAAD%2F2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQE....
  • 好的。正如我已经告诉你的那样,确实是 key=value 对。从中删除 base64= ,您就有了 base64 编码的图像。现在你可以解码了。但是该字符串中有不允许的字符,例如 %2F。这看起来像一个 url 编码的字符。您在发布之前是否对字符串进行了 url 编码?或者你可以在解码base64之前对这个字符串进行url解码。
  • 在我的代码中:httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));我想我应该发送没有 nameValuePairs 的 base64 编码...
  • 好吧,我想我需要发送一个列表...我不能只发送一个字符串

标签: android wcf post imageview


【解决方案1】:

在写入文件之前,您应该尝试在接收端进行 Base64 解码。

【讨论】:

  • 我不知道如何从流中做到这一点
  • 然后从具有 base64 内容的文件中执行此操作。
【解决方案2】:

为了帮助大家,我将发布我的答案,我更改了 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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多