2014-07-02 12 views
6

Ich habe unter Code, um ein Bild von Android auf einen Webserver hochladen.Android-Datei-Upload-Set Inhaltstyp zu Bild

Es funktioniert gut, außer, dass das empfangene Bild Content-Typ Octet Stream ist, und ich brauche es Content Type Image. Irgendeine Idee, wie ich den Inhaltstyp einstellen kann?

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(); 
} 
+0

Sie eine Lösung für diese gefunden? – user1553777

+0

Das ist ein schwieriger. Ich hatte das in Javascript für eine Windows 8 App entwickeln. – Simon

+0

Haben Sie versucht, den Inhaltstyp in der Kopfzeile festzulegen, nachdem Sie Autorisierung festgelegt haben? ac.header ("Inhaltstyp", "application/png"); – isma3l

Antwort

0

diese Zeile hinzufügen zu:

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

Und in Server-Seite, sollten Sie als Mime-Mapping definiert:

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