0

I need to implement a profile picture upload in my app :

How can i upload an photo from the gallery in a a folder ( /uploads) on my server ? Can i do this using my rest api and Volley ?

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == GALLERY && resultCode != 0) {
        Uri mImageUri = data.getData();
        Log.d("TAG", mImageUri.toString());
        try {
            Image = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), mImageUri);
            if (getOrientation(getActivity().getApplicationContext(), mImageUri) != 0) {
                Matrix matrix = new Matrix();
                matrix.postRotate(getOrientation(getActivity().getApplicationContext(), mImageUri));
                if (rotateImage != null)
                    rotateImage.recycle();
                rotateImage = Bitmap.createBitmap(Image, 0, 0, Image.getWidth(), Image.getHeight(), matrix,true);
                bm = rotateImage;
                iv.setImageBitmap(rotateImage);
            } else{
                bm = Image;
                iv.setImageBitmap(Image);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

This is how i set the imageview when the user click on profile picture, but i need top upload/retrieve this.

Hugo Houyez
  • 470
  • 3
  • 19

1 Answers1

0

try this.....

public static JSONObject postFile(String url,String filePath,int id){

 String result="";
 HttpClient httpClient = new DefaultHttpClient();
 HttpPost httpPost = new HttpPost(url);
 File file = new File(filePath);
 MultipartEntity mpEntity = new MultipartEntity();
 ContentBody cbFile = new FileBody(file, "image/jpeg");
 StringBody stringBody= null;
 JSONObject responseObject=null;

 try {

    stringBody = new StringBody(id+"");
    mpEntity.addPart("file", cbFile);
    mpEntity.addPart("id",stringBody);
    httpPost.setEntity(mpEntity);
    System.out.println("executing request " + httpPost.getRequestLine());
    HttpResponse response = httpClient.execute(httpPost);
    HttpEntity resEntity = response.getEntity();
    result=resEntity.toString();
    responseObject=new JSONObject(result);
 } 
 catch (UnsupportedEncodingException e) {
    e.printStackTrace();
 } 
 catch (ClientProtocolException e) {
    e.printStackTrace();
 } 
 catch (IOException e) {
    e.printStackTrace();
 } 
 catch (JSONException e) {
    e.printStackTrace();
 }
  return responseObject;
}
  • What url should i specified ? I mean, do i have to target my api which will make the upload to the /uploads folder of my server ? or can i upload directly into my /folder ? – Hugo Houyez Aug 22 '16 at 11:46
  • The url in your server as http://........com/ulpoad this is your folder previously you are created on server – yosef abu rabeaa Aug 22 '16 at 12:37