2013-06-30 31 views
1

我仍然在学习很多关于Rails和Android开发的知识,所以如果我的问题有点不清楚,请原谅我。使用carrierwave,android和HTTPpost将图像上传到rails应用程序

基本上,我想要做的是上传照片到我的Rails应用程序使用Android应用程序。

我有一个使用Carrierwave和Amazon S3进行图像上传的Rails应用程序。我正在编写一个可用于更新网站条目和上传照片的配套Android应用程序应用程序。我为Rails应用程序创建了一个REST API,以便我可以使用Android应用程序执行http post/get /和delete请求,该应用程序正在更新文本条目。但是我不确定如何进行图片上传,因为当我在Rails日志中查看POST参数时,它包含了很多CarrierWave特定的操作(例如@headers,@content_type,file等)。

任何人都可以推荐一种方式让我开始?

非常感谢!

+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)); 

     } 
    } 

希望帮助!

相关问题