2016-08-22 105 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這是以前你在服務器上創建文件夾 –