gpt4 book ai didi

android - 发送多个图像到服务器 : One image sending thrice to server issue

转载 作者:太空狗 更新时间:2023-10-29 13:15:23 25 4
gpt4 key购买 nike

  • 我必须在服务器端发送三张不同的图片。现在一张图片上传了三次,但我选择了三张不同的图片。

  • 因为我给定了 count=0。然后它拍摄第一张图片并发送第一张图片三次发送到服务器。

  • 如果我给 count=1 那么它需要三次发送第二张图片一次发送到服务器。

  • 我不知道如何一次给出 0,1 和 2 的计数以发送三张图片。这样我就可以一次发送三张图片以将其发送到服务器。

日志:

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

}
}

}



}

任何人都可以帮助我。谢谢。

最佳答案

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\"}";
}

关于android - 发送多个图像到服务器 : One image sending thrice to server issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35336550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com