Uploading an image from Android (with Android Asynchronous Http Client) to rails server (with paperclip)

前端 未结 3 912
误落风尘
误落风尘 2020-12-13 07:47

I\'m trying to upload an image file via http post method from android device to rails server. But posting an image is not working. More specifically, the post parameter (inc

相关标签:
3条回答
  • 2020-12-13 08:07

    Sorry, that was my silly mistake. I should have done

    params.put("picture[image]", new File(Environment.getExternalStorageDirectory().getPath() + "/Pictures/CameraApp/test.jpg"));
    

    not

    params.put("picture[image]", File(Environment.getExternalStorageDirectory().getPath() + "/Pictures/CameraApp/test.jpg"));
    

    Should use New File not File

    When using Android Asynchronous Http Client (http://loopj.com/android-async-http/), we don't have to care about MultipartEntity. Thank you to all guys who answered my question!!!!

    0 讨论(0)
  • 2020-12-13 08:13

    You can upload the Images via the MultipartEntity.

    MultipartEntity, part of HttpMime 4.0 and later. Allows you to put multiple parts, separated by boundary strings and encoded using given charset, into httppost request.

    For more info and how to use Multipart, see this and this.

    0 讨论(0)
  • 2020-12-13 08:16

    There's a description like below, in Android AsyncHttpClient official page (http://loopj.com/android-async-http/)

    "Multipart file uploads with no additional third party libraries"

    and in the section of "Uploading Files with RequestParams", they have a sample code to upload an image

    File myFile = new File("/path/to/file.png");
    RequestParams params = new RequestParams();
    try {
        params.put("profile_picture", myFile);
    } catch(FileNotFoundException e) {}
    

    This is what I did, though didn't work. Does this help answering my question?

    0 讨论(0)
提交回复
热议问题