2016-08-22 101 views
0

我需要实现一个资料图片上传我的应用程序:照片上传与排球和REST API

我如何可以上传一个文件夹(/上传)图库我的服务器上的照片?我可以用我的休息api和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(); 
     } 
    } 
} 

这是我如何设置imageview时,用户点击个人资料图片,但我需要顶部上传/检索这个。

+0

检查这个答案:http://stackoverflow.com/a/30616268/1281775 – Seishin

+0

您是否在寻找一个FTP上传? –

+0

试试我的图书馆https://github.com/amitshekhariitbhu/Fast-Android-Networking –

回答

0

试试这个.....

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; 
} 
+0

的/上传文件夹,我应该指定什么网址?我的意思是,我一定要针对我的API,这将使上传到我的服务器/上传文件夹? 或者我可以直接上传到我的/文件夹? –

+0

在您的服务器为http的网址://........com/ulpoad这是以前你在服务器上创建文件夹 –