0

base64 でエンコードされたイメージを Web ビューから HTTP Post 経由で Web アプリケーションに渡す関数を実装しようとしています。そして、たとえば、この画像をすぐに私のビューに表示する必要があります。このを使用しましたが、適切に機能しなかったため、少しグーグルで調べた後、AsyncTask の使用を開始しました。

public void tryUpload(String url, Context context) {
    new UploadTask(context).execute(url);
}

private class UploadTask extends AsyncTask<String, Void, HttpResponse> {
    private Context context;
    public UploadTask(Context ctx) {
        context = ctx;
    }

    @Override
    protected HttpResponse doInBackground(String... urls) {
        List<NameValuePair> formData = new ArrayList<NameValuePair>(3);
        formData.add(new BasicNameValuePair("image", "base64EncodedString"));

        HttpPost httpPost = new HttpPost(urls[0]);
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(formData, HTTP.UTF_8));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = null;
        try {
            response = httpClient.execute(httpPost);

            WebView browser = (WebView) findViewById(R.id.my_web_engine);
            browser.setWebChromeClient(new MyWebChromeClient());
            browser.setWebViewClient(new MyWebViewClient());

            try{
                String data = new BasicResponseHandler().handleResponse(response);
                browser.loadDataWithBaseURL(urls[0], data, "text/html", HTTP.UTF_8, null);
            } catch (IOException e) {
                e.printStackTrace();
        }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return response;
    }

    @Override
    protected void onPostExecute(HttpResponse response) {
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            String responseBody = null;
            try {
                responseBody = EntityUtils.toString(response.getEntity());
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (responseBody.equalsIgnoreCase("OK"))
                Log.w("myApp", "Everything is OK");
            else
                Log.w("myApp", "Something is wrong");
        }
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (Intent.ACTION_SEND.equals(action) && type != null) {
        if (type.startsWith("image/")) {
                    // for simplicity skipped image encoding to base64
                    String url = getResources().getString(R.string.url) + "/MyController/ImageUpload";
                    Context context = ImageUploadActivity.this;
                    tryUpload(url, context);
            }
        }
    }
}

そして、これが私の ASP.NET MVC アクションです。

// temporary removed authorization
[HttpPost]
public ActionResult ImageUpload(string image)
{
    // skipped some lines
    ViewData.Add("imageFromWebView", image);
    return View();
}

そして、View次の行が含まれています:

<img src="data:image/png;base64,<%: (string)ViewData["imageFromWebView"] %>"/>

を削除する[HttpPost]と、ビューは問題なく読み込まれますが、画像が表示されません。

しかし、私が去る[HttpPost]と、例外が発生します

org.apache.http.client.HttpResponseException: Not Found

次の行で:

String data = new BasicResponseHandler().handleResponse(response);

私が間違っていることを誰かが知っていますか?

4

0 に答える 0