2011-01-24 146 views
6

問候,Android:上傳照片

我在從我的Android手機上傳照片時遇到問題。下面是我使用提示用戶選擇照片的代碼:

public class Uploader{ 
     public void upload(){ 
      Log.i("EOH","hi..."); 
      Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT); 
      photoPickerIntent.setType("image/*"); 
      startActivityForResult(photoPickerIntent, 1); 
     } 
     protected void onActivityResult(int requestCode, int resultCode, Intent data) 
     { 
      if (resultCode == RESULT_OK) 
      { 
       Bundle extras = data.getExtras(); 
       Bitmap b = (Bitmap) extras.get("data"); 
       //what do do now with this Bitmap... 
        //how do I upload it? 




      } 
     } 

    } 

所以我有位圖B,但我不知道下一步該怎麼做?

我發送POST請求以下代碼:

List<NameValuePair> params = new ArrayList<NameValuePair>(2); 
params.add(new BasicNameValuePair("someValA", String.valueOf(lat))); 
params.add(new BasicNameValuePair("someValB", String.valueOf(lng))); 
new HttpConnection(handler).post("http://myurl.com/upload.php",params); 

我怎麼能一個位圖圖像掛鉤到這一點?我搜索谷歌的年齡,無法找到一個好辦法。

我希望有人能幫忙。

很多感謝,


好吧,我已經試過CHIRAG Shah的建議。這裏是我的代碼:

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
     { 
      if (resultCode == RESULT_OK) 
      { 
       Uri imageUri=data.getData(); 
       List<NameValuePair> params = new ArrayList<NameValuePair>(1); 
       params.add(new BasicNameValuePair("image", imageUri.getPath())); 
       post("http://www.myurl.com/ajax/uploadPhoto.php",params); 
      } 
     } 

在帖子函數(推薦):

public void post(String url, List<NameValuePair> nameValuePairs) { 
      HttpClient httpClient = new DefaultHttpClient(); 
      HttpContext localContext = new BasicHttpContext(); 
      HttpPost httpPost = new HttpPost(url); 

      try { 
       MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 

       for(int index=0; index < nameValuePairs.size(); index++) { 
        if(nameValuePairs.get(index).getName().equalsIgnoreCase("image")) { 
         // If the key equals to "image", we use FileBody to transfer the data 
         entity.addPart(nameValuePairs.get(index).getName(), new FileBody(new File (nameValuePairs.get(index).getValue()))); 
        }else{ 
         // Normal string data 
         entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue())); 
        } 
       } 

       httpPost.setEntity(entity); 

       HttpResponse response = httpClient.execute(httpPost, localContext); 
       Log.i("EOH",response.toString()); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
    } 

但是myurl.com/ajax/uploadPhoto.php越來越什麼。我創建了一個PHP日誌來記錄uploadPhoto.php上的任何活動,並且沒有任何內容顯示出來。值得注意的是,如果我只是直接在網頁瀏覽器中輸入myurl.com/ajax/uploadPhoto.php,它會很好地記錄。

還有更多建議嗎?

非常感謝,

+0

我發現所謂的onclick該http帖子必須從異步任務完成,否則他們就不會發生。我一直在尋找類似發佈和獲取數據的東西,並且需要在異步任務中將帖子包裝到url中。 (http://developer.android.com/reference/android/os/AsyncTask.html) – Tim 2013-11-15 00:19:04

回答

0

如果您還不是。嘗試將帖子包裝到異步任務中的服務器並調用任務。我發現我使用的api需要http post/gets遠離主線程,以避免可能的問題鎖定主線程。

將它設置爲異步任務並且開始實際工作相對簡單。

對異步任務的更多信息:http://developer.android.com/reference/android/os/AsyncTask.html

舉個簡單的例子,這是我的異步任務之一:

private class SomeAsyncTask extends AsyncTask<String, Void, JSONObject> { 

    @Override 
    protected JSONObject doInBackground(String... arg0) { 

     UserFunctions uf = new UserFunctions(); 
     JSONObject res = uf.doPostToServer(arg0[0], arg0[1], arg0[2], getApplicationContext()); 

     return res; 
    } 

    @Override 
    protected void onPostExecute(JSONObject result) { 

     try { 
      int success = result.getInt("success"); 
      if(success == 1) { 

       Intent vcact = new Intent(getApplicationContext(), OtherActivity.class); 
       vcact.putExtra("id", cid); 
       startActivity(vcact); 
       // close main screen 
       finish(); 

      } else { 
       Toast.makeText(SomeActivity.this, "Error saving to server. Try again.", Toast.LENGTH_SHORT).show(); 
      } 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 
} 

,並與

new SomeAsyncTask().execute(cid, otherdata, moredata); 
+0

實際的帖子是在uf.doPostToServer中完成的......我認爲你在正確的軌道上,只是嘗試使它異步和看看它是否有幫助。它爲我做了。 – Tim 2013-11-15 00:27:03