• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Help Uploading Multiple Images on server and Getting response null, not upload images on server

lokendrat

Lurker
Apr 27, 2015
1
0
public class UploadActivity extends Activity {

public ProgressDialog dialog;
public MultipartEntity entity;
public ArrayList<String> map = new ArrayList<String>();
Bundle b;

@override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.upload_images);
b = getIntent().getExtras();

if (b != null) {
ArrayList<String> ImgData = b.getStringArrayList("Images");
for (int i = 0; i <ImgData.size()-1; i++) {
map.add(ImgData.get(i).toString());

}


} else {

Toast.makeText(getApplicationContext(), "No Images Available", Toast.LENGTH_LONG).show();
}
System.out.println("Images :" +map.indexOf(0));
}

public void btnUploadPhotoClick(View v){

new ImageUploadTask().execute();


}

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

String sResponse = null;

@override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog = ProgressDialog.show(UploadActivity.this, "Uploading",
"Please wait...", true);
dialog.show();
}

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


System.out.println("Images " + map);


try {

String url = "http://transition2uk.com/ios/ios.php";
//int i = Integer.parseInt(params[0]);
for(int i=0; i<map.size(); i++){

Bitmap bitmap = decodeFile(map.get(i));
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);

entity = new MultipartEntity();


ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 10, bos);
byte[] data = bos.toByteArray();
String base64 = Base64.encodeBytes(data);

entity.addPart("images", new StringBody(base64));
entity.addPart("club_id", new StringBody("media[0]"));
entity.addPart("Uplade Image", new ByteArrayBody(data,"media[0]"));


httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost,
localContext);


//sResponse = EntityUtils.getContentCharSet(response.getEntity());
sResponse = EntityUtils.getContentCharSet(entity);
}
System.out.println("sResponse : " + sResponse);
} catch (Exception e) {
if (dialog.isShowing())
dialog.dismiss();
Log.e(e.getClass().getName(), e.getMessage(), e);

}
return sResponse;
}

@override
protected void onPostExecute(String sResponse) {
try {
if (dialog.isShowing())
dialog.dismiss();

if (sResponse != null) {
Toast.makeText(getApplicationContext(),
sResponse + " Photo uploaded successfully",
Toast.LENGTH_SHORT).show();


}

} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(),
Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}

}
}

public Bitmap decodeFile(String filePath) {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 1024;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, o2);
return bitmap;
}
}
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones