开发者

Help Refactoring working method To Load BitMap (Instead of File) To PHP Server

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.nationwidearbitrationandm开发者_运维技巧ediation.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


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.


Here is a modified version that sends a byte array to the server, as far as you can get the image bytes you can use this to send them to the server as an attached image to the POST request:

public String uploadUserPhoto(byte[] bytesToSend,  boolean isPrimary) {
    String url = "test.php"; 

    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" )));

        reqEntity.addPart(new FormBodyPart("uploadedFile", new ByteArrayBody(bytesToSend, "application/octet-stream", "uploadedFile"))); // you can also use image/jpg as mime type

        post.setEntity(reqEntity);  

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


        return serverResponse;
    } catch (Exception e) {
        System.out.println("Error: " + e.toString());
        return null;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜