0

After Reading a lot of articles, and tons of questions through stackoverflow, I am unable to find answer to my question or situation that I am having until now!

so, I have create an application that have many packages and activities, I used Volley library as a base framework for all my network operations. I successfully be able to retrieve data from server and images and everything is going fine.

I have now this issue: I want to be able to select image from gallery or camera (I also have this and successfully enabled in my application) but I need to post it to server, I really don't have an idea of posting images through server using json, although I can retrieve all images required (more than 100 images) but I still cannot post any to server.

I return to volley Volley presentation and try to understand everything said. but I see it is all about fetching data and not posting ..

so after trying some codes, I try to make volley request (string request) with httpclient (each one in separate) but I nothing work.

so if I need to post image to server, what is the best way? is it by using volley OR by using httpclient regular request within the same application? if it is volley, I've tried This Link and This Question and also this Question

I am very confused ! I am trying to understand this from 3 days and until now I cannot post image ! What should I do? did I miss something or there is something I misunderstand?

Any link or tutorial on this?

Sorry guys for not posting any code, because my tries were only to understand but didn't try any :(

EDIT- I did solve my issue and will share it with public soon ...

Community
  • 1
  • 1
KhalodaRK84
  • 85
  • 2
  • 14
  • I could solve my own question, I did implement another method as the volley do not accept upload for large data, so I used another method described [here](http://androidexample.com/Upload_File_To_Server_-_Android_Example/index.php?view=article_discription&aid=83&aaid=106) and I could successfully upload images token from camera or image file to the requested server ... – KhalodaRK84 Oct 19 '14 at 09:45

1 Answers1

1

Where is your image stored? Is it in the res/ folder? Is it in memory? Is it a file? If you can manage to open an InputStream for the image, using this StringRequest subclass should do the trick:

public class PostFileRequest extends StringRequest {

    private final InputStream mStream;

    public PostFileRequest(final String url,
                           final Response.Listener<String> listener,
                           final Response.ErrorListener errorListener,
                           final InputStream stream) {
        super(Method.POST, url, listener, errorListener);
        mStream = stream;
    }

    @Override
    public byte[] getBody() throws AuthFailureError {
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        streamToStream(mStream, out);
        return out.toByteArray();
    }

    public static void streamToStream(final InputStream in, final OutputStream out) {
        try {
            int c;
            while ((c = in.read()) != -1) {
                out.write(c);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
christiandeange
  • 1,175
  • 12
  • 17
  • cdeange: the Image is not stored yet, I want to take it from gallery of the phone or from camera, which will make a Uri filepath, the filepath is the one I want to use in my code. I appreciate your answer and will try it as soon as possible, thank you – KhalodaRK84 Oct 14 '14 at 06:28