Reputation: 10079
I have to send three different images in server side.Right now one image is uploading thrice.But I have chosen three different image.
Because I had given count=0.then it takes first image and send the first image thrice to the server.
If I give the count=1 then it takes the second image thrice to send to the server at a time.
I don't know how to give a count for 0,1 and 2 at a time to send the three images.So that I can send the three images at a time to send it to the server.
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);
}
}
}
}
Anyone can help me with this.Thank You.
Upvotes: 3
Views: 1246
Reputation: 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]));
You are sending same file three times that's the problem.
Better to put images path in 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 byte array have data then only send it to server
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"));
}
Sending Data on server:
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\"}";
}
Upvotes: 1
Reputation: 1094
If you consider SOLID Principal Well.. I would say.. Instead of passing Strings in Parameter .. Why don't you pass custom Models Array.. This way in future you can use uploader for Different Datatype .. And This gives you flexibility.
For Example
public class Image {
public Bitmap bitmap;
public String dataType;
public String name;
}
And Than You can create your AsynTask
as below.
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);
}
}
}
Upvotes: 0
Reputation: 2790
You are sending the same file that's why you are receiving same image thrice. Look at your code
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]));
In the above code params[1]
refers to the same file pk0.jpg
. If you want to send different images then you should do something like this
for(int i = 0; i < count; i++) {
entity.addPart("filename[" + i + "]", new ByteArrayBody(data,"image/jpeg", "pk" + i + ".jpg"));
}
Hope this helps.
Upvotes: 1