2013-06-30 3 views
1

레일즈 및 안드로이드 개발에 관해서 많은 것을 배우고 있습니다. 내 질문에 약간의 불명예가 있다면 저를 용서해주십시오.carrierwave, android 및 HTTPpost를 사용하여 레일즈 앱에 이미지 업로드

기본적으로 안드로이드 앱을 사용하여 사진을 내 레일 앱에 업로드하고 싶습니다.

이미지 업로드에 Carrierwave 및 Amazon S3를 사용하는 Rails 앱이 있습니다. 나는 사이트의 항목을 업데이트하고 사진을 업로드하는 데 사용할 수있는 Android 응용 프로그램을 작성하고 있습니다. 레일 애플 리케이션을위한 REST API를 만들었고, 텍스트 엔트리를 업데이트하는 안드로이드 앱을 사용하여 http post/get/delete 요청을 수행 할 수있었습니다. 그러나 나는 레일즈 로그에서 POST 매개 변수를 볼 때, @headers, @content_type, file 등과 같은 많은 CarrierWave 특정 동작을 포함하고 있기 때문에 이미지 업로드 작업에 어떻게 접근해야하는지 확신 할 수 없다.

누구나 나를 시작하는 방법을 권할 수 있습니까?

감사합니다.

+0

현재이 문제가 있습니다. 문제를 해결할 수 있었습니까? – bodacious

+0

나는 아래 답변으로 일을 끝낸 것을 올렸다. 궁금한 점이 있으면 알려주세요. – scientiffic

답변

0

나는 코드 스 니펫을 결합하여 효과가있는 것을 얻었습니다.

public class uploadImage extends AsyncTask<Object, Void, HttpEntity>{ 

     @Override 
     protected HttpEntity doInBackground(Object... params){ 
      DefaultHttpClient client = new DefaultHttpClient(); 
      String url= IMAGE_URL+"?auth_token=" + auth_token; 
      Log.d(TAG, "image_url: " + url); 
      HttpPost post = new HttpPost(url); 
      MultipartEntity imageMPentity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 

      try{     
       imageMPentity.addPart("project_id", new StringBody(projectID)); 
       imageMPentity.addPart("step_id", new StringBody(stepID)); 
       imageMPentity.addPart("content_type", new StringBody("image/jpeg")); 
       imageMPentity.addPart("filename", new StringBody(filename)); 
       imageMPentity.addPart("imagePath", new FileBody(new File(filepath)));  

       post.setEntity(imageMPentity);     

      } catch(Exception e){ 
       Log.e(StepActivity.class.getName(), e.getLocalizedMessage(), e); 
      } 
      HttpResponse response = null; 

      try { 
       response = client.execute(post); 
      } catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      HttpEntity result = response.getEntity(); 
      return result; 
     } 

     protected void onPostExecute(HttpEntity result){ 
      if(result !=null){ 
       // add whatever you want it to do next here 
      } 
     }  
    } 

asynctask에 파일 경로와 파일 이름이 필요합니다. 내 응용 프로그램에서는 사용자가 갤러리에서 이미지를 선택할 수있었습니다. 그런 다음 파일 경로와 파일 이름을 검색합니다.

@Override 
// user selects image from gallery 
    protected void onActivityResult(int requestCode, int resultCode, Intent data){ 
     super.onActivityResult(requestCode, resultCode, data); 

     if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data){ 
      Uri selectedImage = data.getData(); 
      String[] filePathColumn = {MediaStore.Images.Media.DATA}; 

      Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); 
      cursor.moveToFirst(); 

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      String picturePath = cursor.getString(columnIndex); 
      Log.d(TAG, "picturePath: " + picturePath); 
      filepath = picturePath; 
      filename = Uri.parse(cursor.getString(columnIndex)).getLastPathSegment().toString(); 
      Log.d(TAG, "filename: " + filename); 

      cursor.close(); 

      // add the image to the view 
      addedImage.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 

     } 
    } 

희망이 있습니다.

관련 문제