views:

13

answers:

1

Hi, This works great for files. I marked the location needing refactoring to get it to work with a bitmap. I can't seem to find a compatible ContentBody for .addPart.

The problem I am trying to solve is that I already have the image in an ImageView within an Activity, so rather than read it again from the SD cara, I want to pass it to this method directly. I will probably scale it while I am at it.

I found a clue in this code, however it does not appear to be Android compliant

    Part[] parts = {
            new FilePart("pictureFile", new 

ByteArrayPartSource("pictureFile", getBytes(bitmap))), };

I'm so freak'n lost :(

Any Ideas? TIA.

public JSONArray uploadUserPhoto(String fileToSend,  boolean isPrimary) {

        String url = "http://www.nationwidearbitrationandmediation.com/ia/android/uploadImage.php"; 
        JSONArray jsonArray = null;

       try {
             HttpClient client = new DefaultHttpClient();  
             client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

             HttpPost post = new HttpPost(url);
             //try adding these
             post.getParams().setParameter("http.protocol.expect-continue", false);
             post.getParams().setParameter("http.protocol.content-charset", "UTF-8"); 
             post.getParams().setParameter("http.protocol.reject-relative-redirect", false);

             MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);  

             reqEntity.addPart("ip", new StringBody(Boolean.toString(isPrimary), "text/plain", Charset.forName( "UTF-8" )));

               /***  NEED HELP HERE ****/
             File fileToUpload = new File(fileToSend);          //get file from name
             Log.d(TAG,"FileSize: " + fileToUpload.length());   //good time to test if file is larger than maxFileSize


             FileBody fileBody = new FileBody(fileToUpload, "image/jpg");
             reqEntity.addPart("uploadedfile", fileBody);
             /*****  DONE *****/

             post.setEntity(reqEntity);  

             HttpResponse response = client.execute(post);  
             HttpEntity resEntity = response.getEntity(); 
             String serverResponse = EntityUtils.toString(resEntity);
             client.getConnectionManager().shutdown();

             Log.i("RESPONSE",serverResponse);

             return  new JSONArray(serverResponse);

    } catch (Exception e) {
        Log.i(TAG,"Error: " + e.toString());
        return jsonArray;
    }

} //uploadUserPhoto

A: 

SOLVED FOR SMALL IMAGES - I used base64 encoding to make image a string. Since I only wanted small images this will work for me.

I am not marking this as answered because others might need a true fix and not a hack.

Jim