我必须在服务器端发送三个不同的图像。现在一个 图像上传了三次。但我选择了三种不同的图像。
因为我给了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);
}
}
}
}
任何人都可以帮助我。谢谢。
答案 0 :(得分: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"));
}
希望这有帮助。
答案 1 :(得分: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\"}";
}
答案 2 :(得分:0)
如果你认为SOLID Principal很好......我会说..而不是在参数中传递字符串..为什么不传递自定义模型数组..这样在将来你可以使用上传器为不同的数据类型..这为您提供了灵活性。
例如
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);
}
}
}