Download Android file for Android.

I have the code below to upload an image from Android to a web server.

It works fine, except that the resulting image is an octet stream of a content type, and I need it to be a content type. Any idea how I can set the content type?

public void sendImage(final Bitmap mBitmap, final String caption, final WebListener webListener) { Thread thread = new Thread(new Runnable() { @Override public void run() { AQuery aq = new AQuery(smApplication); Map<String, Object> params = new HashMap<String, Object>(); params.put("file_name", "name" + (int) (Math.random() * 1000)); params.put("caption", caption); ByteArrayOutputStream blob = new ByteArrayOutputStream(); mBitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, blob); byte[] bitmapdata = blob.toByteArray(); params.put("photo", bitmapdata); AjaxCallback<JSONObject> ac = new AjaxCallback<JSONObject>() { @Override public void callback(String url, JSONObject object, AjaxStatus status) { if (status.getCode() == 200) { try { String success = object.getString("success"); if (success.equals("postCreated")) { webListener.onCompleted(true); } } catch (JSONException e) { webListener.onServiceError(e.getMessage()); } } else { error(status.getError(), webListener); } } }; ac.header("Authorization", "Token token=" + Cache.getInstance().getDeviceKey()); aq.ajax(HOST + API + "posts", params, JSONObject.class, ac, "photoCb"); } }); thread.start(); } 
+6
source share
1 answer

Add this line as well:

 ac.header("Content-Type", "image/png" charset=utf-8"); 

And on the server side, you should define mime-mapping as:

 <mime-mapping> <extension>png</extension> <mime-type>image/png</mime-type> </mime-mapping> 
0
source

Source: https://habr.com/ru/post/971631/


All Articles