2016-02-11 49 views
3
  • 我要送服務器side.Right三種不同的圖像,現在一個 圖像上傳thrice.But我選擇了三個不同的形象。發送多個圖片服務器:一個形象三次發送到服務器問題

  • 因爲我給了count = 0,那麼它需要第一張圖像併發送第一張圖像三次到服務器。

  • 如果我給count = 1,那麼第二張圖像會一次向服務器發送 三次。

  • 我不知道如何給0,1和2一次發送三張圖像。因此,我可以一次發送三張圖像發送給服務器。

logcat的:

02-11 04:32:20.565: E/params[0](16395): 0 
02-11 04:32:20.582: E/ParamsArray(16395): [0, pk0.jpg] 

MainActivity.java:

public class MainActivity extends Activity { 

    private Button upload, pick; 
    MultipartEntity entity; 
    GridView gv; 
    int count = 0; 
    public ArrayList<String> map = new ArrayList<String>(); 
    Bundle b; 
    TextView noImage; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

    upload.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       new ImageUploadTask().execute(count + "", "pk" + count + ".jpg"); 

       /*Log.e("url", url); 
       new UserProfileUpdateAsynTask().execute(url);*/ 
      } 
     }); 
} 

class ImageUploadTask extends AsyncTask<String, Void, String> { 


     ProgressDialog dialog; 
     String url = ""; 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      dialog = new ProgressDialog(MainActivity.this); 
      dialog.setMessage("Loading..."); 
      dialog.setCancelable(false); 
      dialog.show(); 
     } 

     @Override 
     protected String doInBackground(String... params) { 

      HttpEntity resEntity; 

      int i = Integer.parseInt(params[0]); 
      Log.e("params[0]",""+i); 

      Bitmap bitmap = decodeFile(map.get(i)); 
      HttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 
      entity = new MultipartEntity(); 

      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      bitmap.compress(CompressFormat.JPEG, 100, bos); 
      byte[] data = bos.toByteArray(); 

      try { 

       Log.e("ParamsArray",""+Arrays.toString(params)); 



      entity.addPart("filename[0]", new ByteArrayBody(data,"image/jpeg", params[1])); 
      entity.addPart("filename[1]", new ByteArrayBody(data,"image/jpeg", params[1])); 
      entity.addPart("filename[2]", new ByteArrayBody(data,"image/jpeg", params[1])); 


       httpPost.setEntity(entity); 
       HttpResponse response = httpClient.execute(httpPost); 
       resEntity = response.getEntity(); 
       String entityContentAsString = EntityUtils.toString(resEntity); 

       return entityContentAsString; 

      } catch (UnsupportedEncodingException e) { 
       e.printStackTrace(); 
      } catch (ClientProtocolException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      return null; 

     } 

     @Override 
     protected void onPostExecute(String result) { 
      super.onPostExecute(result); 

      if (result != null) { 
       dialog.dismiss(); 
       Log.e("Update_profile", result); 

      } 
     } 

    } 



} 

任何人都可以幫我this.Thank你。

回答

1
entity.addPart("filename[0]", new ByteArrayBody(data,"image/jpeg", params[1])); 
entity.addPart("filename[1]", new ByteArrayBody(data,"image/jpeg", params[1])); 
entity.addPart("filename[2]", new ByteArrayBody(data,"image/jpeg", params[1])); 

您發送相同的文件三倍這就是問題所在。


更好地把在SharedPrefrence圖像路徑。

byte[] data1 = null,data2= null,data3= null; 

    if(PreferenceManager.getDefaultSharedPreferences(getBaseContext()).contains("endum_image_0")) 
    {  up_image1 = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("endum_image_0", ""); 
      bitmap = BitmapFactory.decodeFile(up_image1, options1); 
      bitmap.compress(CompressFormat.JPEG, 100, bos1); 
      data1 = bos1.toByteArray(); 
    } 
    if(PreferenceManager.getDefaultSharedPreferences(getBaseContext()).contains("endum_image_1")) 
    {  up_image2 = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("endum_image_1", ""); 
      bitmap = BitmapFactory.decodeFile(up_image2, options1); 
      bitmap.compress(CompressFormat.JPEG, 100, bos2); 
      data2 = bos2.toByteArray(); 
    } 

    if(PreferenceManager.getDefaultSharedPreferences(getBaseContext()).contains("endum_image_2")) 
    {  up_image3 = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("endum_image_2", ""); 
      bitmap = BitmapFactory.decodeFile(up_image3, options1); 
      bitmap.compress(CompressFormat.JPEG, 100, bos3); 
      data3 = bos3.toByteArray(); 
    } 

如果字節數組有數據,則只有將其發送到服務器

if(data1!=null){ 
    entity.addPart("files[]", new ByteArrayBody(data1,"image/jpeg", "u1.jpg")); 
    } 
    if(data2!=null){ 
    entity.addPart("files[]", new ByteArrayBody(data2,"image/jpeg", "u2.jpg")); 
    } 

    if(data3!=null){ 
    entity.addPart("files[]", new ByteArrayBody(data3,"image/jpeg", "u3.jpg")); 
    } 

上服務器發送數據:

HttpClient httpClient = new DefaultHttpClient(); 
    HttpPost httpPost = new HttpPost(url); 
    MultipartEntity entity = new MultipartEntity(); 
    httpPost.setEntity(entity); 
    HttpResponse response = httpClient.execute(httpPost); 

    BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")); 

    StringBuilder s = new StringBuilder(); 

    while ((sResponse = reader.readLine()) != null) 
     { 
      s = s.append(sResponse); 
     } 

    if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) 
    { 
     return s.toString(); 
    }else 
    { 
     return "{\"status\":\"false\",\"message\":\"Some error occurred\"}"; 
    } 
1

您正在發送相同的文件,這就是爲什麼你收到相同的圖像三次。看看你的代碼

entity.addPart("filename[0]", new ByteArrayBody(data,"image/jpeg", params[1])); 
      entity.addPart("filename[1]", new ByteArrayBody(data,"image/jpeg", params[1])); 
      entity.addPart("filename[2]", new ByteArrayBody(data,"image/jpeg", params[1])); 

在上面的代碼params[1]指的是同一個文件pk0.jpg。如果你想發送不同的圖像,那麼你應該做這樣的事情

for(int i = 0; i < count; i++) { 
entity.addPart("filename[" + i + "]", new ByteArrayBody(data,"image/jpeg", "pk" + i + ".jpg")); 
} 

希望這有助於。

+0

現在它沒有發送給服務器的任何圖像.initially int count = 0;這就是爲什麼它不是在for循環中執行,然後我設置我<3在循環中。它三次拍攝三個圖像。 – Steve

+0

請顯示您用於保存目的的服務器代碼。它看起來像服務器正在複製。 –

0

如果考慮SOLID主要嗯..我會說..不是傳遞字符串參數中的..你爲什麼不通過定製機型陣列。這種方式在將來你可以使用不同的數據類型上載。而這給你靈活性。

對於實例

public class Image { 
public Bitmap bitmap; 
public String dataType; 
public String name; 
} 

比你可以創建你AsynTask如下。

class ImageUploadTask extends AsyncTask<Image, Void, String> { 
     ProgressDialog dialog; 
     String url = ""; 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      dialog = new ProgressDialog(MainActivity.this); 
      dialog.setMessage("Loading..."); 
      dialog.setCancelable(false); 
      dialog.show(); 
     } 

     @Override 
     protected String doInBackground(Image... params) { 
      HttpEntity resEntity; 
      HttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 
      entity = new MultipartEntity(); 


      try { 

      for(int i;i<params.length;i++) { 
       ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
       params[i].bitmap.compress(CompressFormat.JPEG, 100, bos); 
       byte[] data = bos.toByteArray(); 
       entity.addPart("filename["+i+"]", new ByteArrayBody(data,params[i].dataType, params[i].name)); 
      } 




       httpPost.setEntity(entity); 
       HttpResponse response = httpClient.execute(httpPost); 
       resEntity = response.getEntity(); 
       String entityContentAsString = EntityUtils.toString(resEntity); 

       return entityContentAsString; 

      } catch (UnsupportedEncodingException e) { 
       e.printStackTrace(); 
      } catch (ClientProtocolException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      return null; 

     } 

     @Override 
     protected void onPostExecute(String result) { 
      super.onPostExecute(result); 

      if (result != null) { 
       dialog.dismiss(); 
       Log.e("Update_profile", result); 

      } 
     } 

    }